public static <T> T assertParseExampleFile(String fileName, Class<T> clazz) throws Exception { File exampleFile = new File(getKubernetesExamplesDir(), fileName); assertFileExists(exampleFile); T answer = OBJECT_MAPPER.readerFor(clazz).readValue(exampleFile); assertNotNull("Null returned while unmarshalling " + exampleFile, answer); LOG.info("Parsed: " + fileName + " as: " + answer); return answer; }
private static <T> T fromJSON(ObjectMapper mapper, String json, Class<T> expectedType) { try { return mapper.readerFor(expectedType).readValue(json); } catch (IOException e) { throw new IllegalArgumentException( "Cannot unmarshal JSON as " + expectedType.getSimpleName(), e); } }
/** * @param json A valid JSON stream, may be <code>null</code>. * @return The {@link DataSetMetadata} instance parsed from stream or <code>null</code> if * parameter is null. If stream is empty, also returns <code>null</code>. */ public DataSet from(InputStream json) { try { final ObjectMapper mapper = builder.build(); JsonParser parser = mapper.getFactory().createParser(json); return mapper.readerFor(DataSet.class).readValue(parser); } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e); } }
/** * Call the version service on the given service: dataset, preparation or transformation. * * @param serviceName the name of the service * @return the version of the called service */ private Version callVersionService(String serviceUrl, String serviceName) { HystrixCommand<InputStream> versionCommand = getCommand(VersionCommand.class, serviceUrl); try (InputStream content = versionCommand.execute()) { final Version version = mapper.readerFor(Version.class).readValue(content); version.setServiceName(serviceName); return version; } catch (IOException e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_GET_SERVICE_VERSION, e); } }
public void testMediaItemSimple() throws Exception { ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_MEDIA_ITEM); final ObjectWriter w = MAPPER.writer(schema); byte[] bytes = w.writeValueAsBytes(MediaItem.buildItem()); assertNotNull(bytes); assertEquals(252, bytes.length); // let's read back for fun MediaItem output = MAPPER.readerFor(MediaItem.class).with(schema).readValue(bytes); assertNotNull(output); }
public static Personnage getPersonnage(final String name, final String realm) { String readCharacter = queryBattle(BattleApiConstants.getCharacterQueryUrl(name, realm)); LOGGER.debug(readCharacter); if (readCharacter == null) { return null; } Personnage p = new Personnage(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(Personnage.class); p = reader.readValue(readCharacter); } catch (Exception e) { LOGGER.error(e); } return p; }
public static Guilde getGuilde() { String readGuildMembersCount = queryBattle(BattleApiConstants.getGuildeMembersQueryUrl()); LOGGER.debug(readGuildMembersCount); if (readGuildMembersCount == null) { return null; } readGuildMembersCount = readGuildMembersCount.substring(readGuildMembersCount.indexOf("(") + 1); readGuildMembersCount = readGuildMembersCount.substring(0, readGuildMembersCount.length() - 2); Guilde g = new Guilde(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(Guilde.class); g = reader.readValue(readGuildMembersCount); LOGGER.info("Nombre de membres " + g.getMembers().size()); } catch (Exception e) { LOGGER.error(e); } return g; }
@Test public void should_iterate_row_with_metadata() throws IOException { // given String[] columnNames = new String[] { "id", "firstname", "lastname", "state", "registration", "city", "birth", "nbCommands", "avgAmount" }; final InputStream input = this.getClass().getResourceAsStream("dataSetRowMetadata.json"); final ObjectMapper mapper = builder.build(); try (JsonParser parser = mapper.getFactory().createParser(input)) { final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser); final Iterator<DataSetRow> iterator = dataSet.getRecords().iterator(); List<ColumnMetadata> actualColumns = new ArrayList<>(); int recordCount = 0; while (iterator.hasNext()) { final DataSetRow next = iterator.next(); actualColumns = next.getRowMetadata().getColumns(); assertThat(actualColumns, not(empty())); recordCount++; } // then assertEquals(10, recordCount); for (int i = 0; i < actualColumns.size(); i++) { final ColumnMetadata column = actualColumns.get(i); assertEquals(columnNames[i], column.getId()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e); } }
public Stream<T> parse(InputStream is, CsvErrorSniffer context) { ObjectMapper mapper = objectMapper.copy(); formatter.initMixIn(mapper); ObjectReader reader = mapper.readerFor(formatter.getTargetClass()); CsvSchema schema = new CsvSchema(formatter); return parseToCsvLine(is) .map( line -> { line.getException() .ifPresent( e -> context.mark(new Location(line.getLineNumber(), OptionalInt.empty()))); Set<String> ignoreField = new HashSet<>(); while (true) { try { return reader.readValue(schema.toJson(line, ignoreField)); } catch (JsonMappingException e) { String path = buildPath(e.getPath()); ; Location location = new Location( line.getLineNumber(), OptionalInt.of(schema.getColumnNumber(path))); if (context.contains(location)) { throw new IllegalStateException("invalid row state: " + e.getLocation()); } context.mark(location); ignoreField.add(path); } catch (IOException e) { context.mark(new Location(line.getLineNumber(), OptionalInt.empty())); try { return formatter.getTargetClass().newInstance(); } catch (ReflectiveOperationException e2) { throw new ReflectiveOperationRuntimeException(e2); } } } }); }
private static List<Race> getListRaces() { String sRaces = queryBattle(BattleApiConstants.getRacesQueryUrl()); LOGGER.debug(sRaces); if (sRaces == null) { return null; } List<Race> races = new ArrayList<Race>(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(new TypeReference<List<Race>>() {}).withRootName("races"); races = reader.readValue(sRaces); LOGGER.debug("Nombre de races " + races.size()); for (Race race : races) { LOGGER.debug(race.toString()); } } catch (Exception e) { LOGGER.error(e); } return races; }
static { final ObjectMapper mapper = new ObjectMapper(); joWriter = mapper.writer(); joReader = mapper.readerFor(MyClass.class); }