List of usage examples for com.fasterxml.jackson.databind MappingJsonFactory MappingJsonFactory
public MappingJsonFactory()
From source file:net.floodlightcontroller.loadbalancer.MonitorsResource.java
protected LBMonitor jsonToMonitor(String json) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp;/*w w w . j a va2 s .c o m*/ LBMonitor monitor = new LBMonitor(); try { jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; else if (n.equals("monitor")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { monitor.id = jp.getText(); continue; } if (field.equals("name")) { monitor.name = jp.getText(); continue; } if (field.equals("type")) { monitor.type = Short.parseShort(jp.getText()); continue; } if (field.equals("delay")) { monitor.delay = Short.parseShort(jp.getText()); continue; } if (field.equals("timeout")) { monitor.timeout = Short.parseShort(jp.getText()); continue; } if (field.equals("attempts_before_deactivation")) { monitor.attemptsBeforeDeactivation = Short.parseShort(jp.getText()); continue; } if (field.equals("network_id")) { monitor.netId = jp.getText(); continue; } if (field.equals("address")) { monitor.address = Integer.parseInt(jp.getText()); continue; } if (field.equals("protocol")) { monitor.protocol = Byte.parseByte(jp.getText()); continue; } if (field.equals("port")) { monitor.port = Short.parseShort(jp.getText()); continue; } if (field.equals("admin_state")) { monitor.adminState = Short.parseShort(jp.getText()); continue; } if (field.equals("status")) { monitor.status = Short.parseShort(jp.getText()); continue; } log.warn("Unrecognized field {} in " + "parsing Vips", jp.getText()); } } } jp.close(); return monitor; }
From source file:org.apache.arrow.vector.file.json.JsonFileWriter.java
public JsonFileWriter(File outputFile, JSONWriteConfig config) throws IOException { MappingJsonFactory jsonFactory = new MappingJsonFactory(); this.generator = jsonFactory.createGenerator(outputFile, JsonEncoding.UTF8); if (config.pretty) { DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(NopIndenter.instance); this.generator.setPrettyPrinter(prettyPrinter); }/*from w w w . j ava2s . c om*/ }
From source file:org.apache.arrow.vector.file.json.JsonFileReader.java
public JsonFileReader(File inputFile, BufferAllocator allocator) throws JsonParseException, IOException { super();/*ww w . ja v a 2s . c om*/ this.inputFile = inputFile; this.allocator = allocator; MappingJsonFactory jsonFactory = new MappingJsonFactory(); this.parser = jsonFactory.createParser(inputFile); }
From source file:org.jberet.support.io.MappingJsonFactoryObjectFactory.java
/** * Gets an instance of {@code com.fasterxml.jackson.databind.MappingJsonFactory} based on the resource configuration * in the application server. The parameter {@code environment} contains MappingJsonFactory configuration properties, * and accepts the following properties: * <ul>// w ww.j ava 2 s . c o m * <li>jsonFactoryFeatures: JsonFactory features as defined in com.fasterxml.jackson.core.JsonFactory.Feature * <li>mapperFeatures: ObjectMapper features as defined in com.fasterxml.jackson.databind.MapperFeature * <li>deserializationFeatures: * <li>serializationFeatures: * <li>customDeserializers: * <li>customSerializers: * <li>deserializationProblemHandlers: * <li>customDataTypeModules: * <li>inputDecorator: fully-qualified name of a class that extends {@code com.fasterxml.jackson.core.io.InputDecorator} * <li>outputDecorator: fully-qualified name of a class that extends {@code com.fasterxml.jackson.core.io.OutputDecorator} * </ul> * * @param obj the JNDI name of {@code com.fasterxml.jackson.databind.MappingJsonFactory} resource * @param name always null * @param nameCtx always null * @param environment a {@code Hashtable} of configuration properties * @return an instance of {@code com.fasterxml.jackson.databind.MappingJsonFactory} * @throws Exception any exception occurred */ @Override public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception { MappingJsonFactory jsonFactory = jsonFactoryCached; if (jsonFactory == null) { synchronized (this) { jsonFactory = jsonFactoryCached; if (jsonFactory == null) { jsonFactoryCached = jsonFactory = new MappingJsonFactory(); } final ClassLoader classLoader = MappingJsonFactoryObjectFactory.class.getClassLoader(); final ObjectMapper objectMapper = jsonFactory.getCodec(); final Object jsonFactoryFeatures = environment.get("jsonFactoryFeatures"); if (jsonFactoryFeatures != null) { NoMappingJsonFactoryObjectFactory.configureJsonFactoryFeatures(jsonFactory, (String) jsonFactoryFeatures); } final Object mapperFeatures = environment.get("mapperFeatures"); if (mapperFeatures != null) { configureMapperFeatures(objectMapper, (String) mapperFeatures); } final Object deserializationFeatures = environment.get("deserializationFeatures"); if (deserializationFeatures != null) { configureDeserializationFeatures(objectMapper, (String) deserializationFeatures); } final Object serializationFeatures = environment.get("serializationFeatures"); if (serializationFeatures != null) { configureSerializationFeatures(objectMapper, (String) serializationFeatures); } final Object deserializationProblemHandlers = environment.get("deserializationProblemHandlers"); if (deserializationProblemHandlers != null) { configureDeserializationProblemHandlers(objectMapper, (String) deserializationProblemHandlers, classLoader); } configureCustomSerializersAndDeserializers(objectMapper, (String) environment.get("customSerializers"), (String) environment.get("customDeserializers"), (String) environment.get("customDataTypeModules"), classLoader); NoMappingJsonFactoryObjectFactory.configureInputDecoratorAndOutputDecorator(jsonFactory, environment); } } return jsonFactory; }
From source file:org.apache.metamodel.json.JsonDataContext.java
private DocumentSource createDocumentSource() { final InputStream inputStream = _resource.read(); try {/*from www .ja v a2 s . c o m*/ final MappingJsonFactory jsonFactory = new MappingJsonFactory(); final JsonParser parser = jsonFactory.createParser(inputStream); logger.debug("Created JSON parser for resource: {}", _resource); return new JsonDocumentSource(parser, _resource.getName()); } catch (Exception e) { FileHelper.safeClose(inputStream); throw new MetaModelException("Unexpected error while creating JSON parser", e); } }
From source file:com.crossover.trial.weather.domain.AirportTest.java
@Test public void testSerialization() throws IOException { JsonFactory jsonFactory = new MappingJsonFactory(); Airport airport = new Airport.Builder().withIata("aaa").withLatitude(10).withLongitude(20).build(); StringWriter out = new StringWriter(); JsonGenerator json = jsonFactory.createGenerator(out); json.writeObject(airport);/* www . j a v a 2 s . com*/ json.close(); assertThat(out.toString(), equalTo("{\"iata\":\"AAA\",\"latitude\":10.0,\"longitude\":20.0}")); }
From source file:can.yrt.oba.serialization.JacksonSerializer.java
public String serialize(Object obj) { StringWriter writer = new StringWriter(); JsonGenerator jsonGenerator;//from w w w . j ava 2s . c om try { jsonGenerator = new MappingJsonFactory().createJsonGenerator(writer); mMapper.writeValue(jsonGenerator, obj); return writer.toString(); } catch (JsonGenerationException e) { Log.e(TAG, e.toString()); return getErrorJson(ObaApi.OBA_INTERNAL_ERROR, e.toString()); } catch (JsonMappingException e) { Log.e(TAG, e.toString()); return getErrorJson(ObaApi.OBA_INTERNAL_ERROR, e.toString()); } catch (IOException e) { Log.e(TAG, e.toString()); return getErrorJson(ObaApi.OBA_IO_EXCEPTION, e.toString()); } }
From source file:com.crossover.trial.weather.domain.AirportTest.java
@Test public void testDeserialization() throws IOException { JsonFactory jsonFactory = new MappingJsonFactory(); Airport airport1 = new Airport.Builder().withIata("aaa").withLatitude(10).withLongitude(20).build(); Airport airport2 = jsonFactory.createParser("{\"iata\":\"AAA\",\"latitude\":10.0,\"longitude\":20.0}") .readValueAs(Airport.class); assertThat(airport1, equalTo(airport2)); }
From source file:org.jberet.support.io.MongoItemReaderTest.java
static void addTestData(final String dataResource, final String mongoCollection, final int minSizeIfExists) throws Exception { final DBCollection collection = db.getCollection(mongoCollection); if (collection.find().count() >= minSizeIfExists) { System.out.printf("The readCollection %s already contains 100 items, skip adding test data.%n", mongoCollection);//from ww w . j av a 2 s.c om return; } InputStream inputStream = MongoItemReaderTest.class.getClassLoader().getResourceAsStream(dataResource); if (inputStream == null) { try { final URL url = new URI(dataResource).toURL(); inputStream = url.openStream(); } catch (final Exception e) { System.out.printf("Failed to convert dataResource %s to URL: %s%n", dataResource, e); } } if (inputStream == null) { throw new IllegalStateException("The inputStream for the test data is null"); } final JsonFactory jsonFactory = new MappingJsonFactory(); final JsonParser parser = jsonFactory.createParser(inputStream); final JsonNode arrayNode = parser.readValueAs(ArrayNode.class); final Iterator<JsonNode> elements = arrayNode.elements(); final List<DBObject> dbObjects = new ArrayList<DBObject>(); while (elements.hasNext()) { final DBObject dbObject = (DBObject) JSON.parse(elements.next().toString()); dbObjects.add(dbObject); } collection.insert(dbObjects); }
From source file:org.jberet.support.io.JsonItemReaderWriterBase.java
/** * Initializes {@code JsonFactory} or its subtypes, e.g., {@code com.fasterxml.jackson.dataformat.xml.XmlFactory}, * or {@code com.fasterxml.jackson.dataformat.csv.CsvFactory}. The factory should be initialized with a valid * {@code ObjectMapper}.// w ww. j av a2s . c om * * Subclass may override this method to use different concrete types of {@code JsonFactory}. * * @throws Exception * @since 1.2.0.Alpha1 */ protected void initJsonFactory() throws Exception { if (jsonFactoryLookup != null) { jsonFactory = InitialContext.doLookup(jsonFactoryLookup); } else { jsonFactory = new MappingJsonFactory(); } }