public GenericJacksonWayGraphOutputFormat(ISegmentOutputFormat<T> segmentOutputFormat, IAdapter<IGraphVersionMetadataDTO, IWayGraphVersionMetadata> adapter, OutputStream stream, JsonGenerator generator) { this.segmentOutputFormat = segmentOutputFormat; this.adapter = adapter; if (generator == null) { try { this.generator = new MappingJsonFactory().createGenerator(stream, JsonEncoding.UTF8); this.generator.useDefaultPrettyPrinter(); } catch (IOException e) { log.error("error creating jackson json factory", e); } } else { this.generator = generator; } }
public GenericJacksonSegmentOutputFormat(ISegmentAdapterRegistry<? extends IBaseSegmentDTO, T> adapterRegistry, OutputStream stream, JsonGenerator generator, int flushBatchCount) { this.adapterRegistry = adapterRegistry; if (generator == null) { try { this.generator = new MappingJsonFactory().createGenerator(new BufferedOutputStream(stream), JsonEncoding.UTF8); this.generator.useDefaultPrettyPrinter(); } catch (IOException e) { log.error("error creating jackson json factory", e); } } if (flushBatchCount > 0 ) { this.flushBatchCount = flushBatchCount; } else { log.warn("flushBatchCount ignored, can not be negative or 0"); } }
/** * Extracts subnet mask from a JSON string * @param fmJson The JSON formatted string * @return The subnet mask * @throws IOException If there was an error parsing the JSON */ public static String jsonExtractSubnetMask(String fmJson) throws IOException { String subnet_mask = ""; MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createParser(fmJson); } 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; if (n == "subnet-mask") { subnet_mask = jp.getText(); break; } } return subnet_mask; }
@Override public Data<Resource> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); ObjectMapper mapper = new MappingJsonFactory().getCodec(); if (node.isArray()) { List<Resource> resources = new ArrayList<>(); for (JsonNode n : node) { Resource r = mapper.convertValue(n, Resource.class); resources.add(r); } return new Data<>(resources); } Resource resource = mapper.convertValue(node, Resource.class); return new Data<>(resource); }
@Bean public HandlerMapping wampWebSocketHandlerMapping() { WebSocketHandler handler = subProtocolWebSocketHandler(); handler = decorateWebSocketHandler(handler); WebMvcWampEndpointRegistry registry = new WebMvcWampEndpointRegistry(handler, getTransportRegistration(), messageBrokerSockJsTaskScheduler(), new MappingJsonFactory(lookupObjectMapper())); List<HandshakeInterceptor> handshakeInterceptors = new ArrayList<>(); addHandshakeInterceptors(handshakeInterceptors); registry.addHandshakeInterceptors(handshakeInterceptors); registerWampEndpoints(registry); return registry.getHandlerMapping(); }
public WampClient(DataFormat dataFormat) { this.isBinary = dataFormat != DataFormat.JSON; this.result = new CompletableFutureWebSocketHandler(); this.headers = new WebSocketHttpHeaders(); switch (dataFormat) { case CBOR: this.jsonFactory = new ObjectMapper(new CBORFactory()).getFactory(); this.headers.setSecWebSocketProtocol(WampSubProtocolHandler.CBOR_PROTOCOL); break; case MSGPACK: this.jsonFactory = new ObjectMapper(new MessagePackFactory()).getFactory(); this.headers.setSecWebSocketProtocol(WampSubProtocolHandler.MSGPACK_PROTOCOL); break; case JSON: this.jsonFactory = new MappingJsonFactory(new ObjectMapper()); this.headers.setSecWebSocketProtocol(WampSubProtocolHandler.JSON_PROTOCOL); break; case SMILE: this.jsonFactory = new ObjectMapper(new SmileFactory()).getFactory(); this.headers.setSecWebSocketProtocol(WampSubProtocolHandler.SMILE_PROTOCOL); break; default: this.jsonFactory = null; } }
public CompletableFutureWebSocketHandler(int expectedNoOfResults) { this.jsonFactory = new MappingJsonFactory(new ObjectMapper()); this.msgpackFactory = new ObjectMapper(new MessagePackFactory()).getFactory(); this.cborFactory = new ObjectMapper(new CBORFactory()).getFactory(); this.smileFactory = new ObjectMapper(new SmileFactory()).getFactory(); this.timeout = getTimeoutValue(); this.welcomeMessageFuture = new CompletableFuture<>(); this.reset(expectedNoOfResults); }
public GenericJacksonSegmentOutputFormat(ISegmentAdapterRegistry<? extends IBaseSegmentDTO, T> adapterRegistry, OutputStream stream, JsonGenerator generator) { this.adapterRegistry = adapterRegistry; if (generator == null) { try { this.generator = new MappingJsonFactory().createGenerator(stream, JsonEncoding.UTF8); this.generator.useDefaultPrettyPrinter(); } catch (IOException e) { log.error("error creating jackson json factory", e); } } else { this.generator = generator; } }
@Test public void testSerialize() throws IOException { String test = "[{\"name\":\"HalloHallo\",\"segmentId\":12345}]"; InputStream stream = new ByteArrayInputStream( test.getBytes() ); JsonFactory factory = new MappingJsonFactory(); JsonParser parser = factory.createParser(stream); JsonToken token = parser.nextToken(); if (token == JsonToken.START_ARRAY) { do { parser.nextToken(); DefaultSegmentXInfoDTO segmentXInfoDTO = parser.readValueAs(DefaultSegmentXInfoDTO.class); } while (token == JsonToken.END_ARRAY); } }
@Override public String convert(final List<ComplexNestedType> object) { try { StringWriter writer = new StringWriter(); JsonFactory jsonFactory = new MappingJsonFactory(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer); jsonGenerator.writeObject(object); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public List<ComplexNestedType> unconvert(String obj) { try { JsonFactory jsonFactory = new MappingJsonFactory(); JsonParser jsonParser = jsonFactory.createJsonParser(new StringReader(obj)); return jsonParser.readValueAs(new TypeReference<List<ComplexNestedType>>() { }); } catch (Exception e) { throw new RuntimeException(e); } }
protected void jsonToHostDefinition(String json, HostDefinition host) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createParser(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("attachment")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { host.attachment = jp.getText(); } else if (field.equals("mac")) { host.mac = jp.getText(); } } } } jp.close(); }
/** * Gets the entry name of a flow mod * @param fmJson The OFFlowMod in a JSON representation * @return The name of the OFFlowMod, null if not found * @throws IOException If there was an error parsing the JSON */ public static String getEntryNameFromJson(String fmJson) throws IOException{ MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createParser(fmJson); } 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; if (n == StaticFlowEntryPusher.COLUMN_NAME) return jp.getText(); } return null; }
protected void jsonToHostDefinition(String json, HostDefinition host) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; 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("attachment")) { while (jp.nextToken() != JsonToken.END_OBJECT) { String field = jp.getCurrentName(); if (field.equals("id")) { host.attachment = jp.getText(); } else if (field.equals("mac")) { host.mac = jp.getText(); } } } } jp.close(); }
/** * Gets the entry name of a flow mod * @param fmJson The OFFlowMod in a JSON representation * @return The name of the OFFlowMod, null if not found * @throws IOException If there was an error parsing the JSON */ public static String getEntryNameFromJson(String fmJson) throws IOException{ MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createJsonParser(fmJson); } 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; if (n == StaticFlowEntryPusher.COLUMN_NAME) return jp.getText(); } return null; }
/** * Extracts subnet mask from a JSON string * @param fmJson The JSON formatted string * @return The subnet mask * @throws IOException If there was an error parsing the JSON */ public static String jsonExtractSubnetMask(String fmJson) throws IOException { String subnet_mask = ""; MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createJsonParser(fmJson); } 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; if (n == "subnet-mask") { subnet_mask = jp.getText(); break; } } return subnet_mask; }
/** * Gets the entry name of a flow mod * @param fmJson The OFFlowMod in a JSON representation * @return The name of the OFFlowMod, null if not found * @throws IOException If there was an error parsing the JSON */ public static String getEntryNameFromJson(String fmJson) throws IOException{ MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; try { jp = f.createJsonParser(fmJson); } 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; if (n == "name") return jp.getText(); } return null; }
@Override public JsonNode apply(Response response) { MappingJsonFactory mappingJsonFactory = new MappingJsonFactory(); ObjectNode objectNode = JsonNodeFactory.instance.objectNode(); objectNode.set(DruidJsonResponseContentKeys.RESPONSE.getName(), baseStrategy.apply(response)); try { objectNode.set( DruidJsonResponseContentKeys.DRUID_RESPONSE_CONTEXT.getName(), mappingJsonFactory .createParser( response.getHeader(DruidJsonResponseContentKeys.DRUID_RESPONSE_CONTEXT.getName()) ) .readValueAsTree() ); int statusCode = response.getStatusCode(); objectNode.set( DruidJsonResponseContentKeys.STATUS_CODE.getName(), mappingJsonFactory.createParser(String.valueOf(statusCode)).readValueAsTree() ); if (CacheFeatureFlag.ETAG.isOn() && statusCode == OK.getStatusCode()) { objectNode.set( DruidJsonResponseContentKeys.ETAG.getName(), mappingJsonFactory .createParser( response.getHeader(DruidJsonResponseContentKeys.ETAG.getName()) ) .readValueAsTree() ); } } catch (IOException ioe) { throw new IllegalStateException(ioe); } return objectNode; }
@Override public JsonNode apply(Response response) { try { return new MappingJsonFactory().createParser(response.getResponseBodyAsStream()).readValueAsTree(); } catch (IOException ioe) { throw new IllegalStateException(ioe); } }