Java Code Examples for com.fasterxml.jackson.core.JsonParser#setCodec()
The following examples show how to use
com.fasterxml.jackson.core.JsonParser#setCodec() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudErrorDeserializer.java From botbuilder-java with MIT License | 6 votes |
@Override public CloudError deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { p.setCodec(mapper); JsonNode errorNode = p.readValueAsTree(); if (errorNode == null) { return null; } if (errorNode.get("error") != null) { errorNode = errorNode.get("error"); } JsonParser parser = new JsonFactory().createParser(errorNode.toString()); parser.setCodec(mapper); return parser.readValueAs(CloudError.class); }
Example 2
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testResponse() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "ModelTest.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); ResponseDeserializer responseDeserializer = new ResponseDeserializer(); try { responseDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example 3
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializeSecurityDef() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Security.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); SecurityDefinitionDeserializer securityDefinitionDeserializer = new SecurityDefinitionDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { securityDefinitionDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example 4
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializeModel() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "ModelTest.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); ModelDeserializer modelDeserializer = new ModelDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(modelDeserializer.deserialize(jsonParser, deserializationContext)); }
Example 5
Source File: JacksonResponse.java From nifi with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings("unchecked") public <T> T readEntity(Class<T> entityType) { if (InputStream.class.equals(entityType)) { return (T) new ByteArrayInputStream(responseBody); } if (String.class.equals(entityType)) { return (T) new String(responseBody, StandardCharsets.UTF_8); } try { final JsonParser parser = jsonFactory.createParser(responseBody); parser.setCodec(codec); return parser.readValueAs(entityType); } catch (final Exception e) { throw new RuntimeException("Failed to parse response as entity of type " + entityType, e); } }
Example 6
Source File: ComponentXODeserializerTest.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
@Test public void deserialize() throws IOException { ComponentXO componentXO = new FooComponentXO(new BarComponentXO(new DefaultComponentXO())); when(componentXOFactory.createComponentXO()).thenReturn(componentXO); String json = "{\"id\": \"theid\", \"foo\": \"fiz\", \"bar\": \"biz\"}"; JsonParser jsonParser = jsonFactory.createParser(json); jsonParser.setCodec(objectMapper); ComponentXO result = underTest.deserialize(jsonParser, deserializationContext); assertThat(result, notNullValue()); assertThat(result.getId(), equalTo("theid")); FooComponentXO fooComponentXO = getDecoratedEntity(result, FooComponentXO.class); assertThat(fooComponentXO, notNullValue()); assertThat(fooComponentXO.getFoo(), equalTo("fiz")); BarComponentXO barComponentXO = getDecoratedEntity(result, BarComponentXO.class); assertThat(barComponentXO, notNullValue()); assertThat(barComponentXO.getBar(), equalTo("biz")); }
Example 7
Source File: JsonldBeanDeserializerModifier.java From jackson-jsonld with MIT License | 6 votes |
private Object parseJsonldObject(JsonParser jp) throws IOException { Object rval = null; final JsonToken initialToken = jp.getCurrentToken(); if (initialToken == JsonToken.START_ARRAY) { jp.setCodec(mapper); rval = jp.readValueAs(List.class); } else if (initialToken == JsonToken.START_OBJECT) { jp.setCodec(mapper); rval = jp.readValueAs(Map.class); } else if (initialToken == JsonToken.VALUE_STRING) { jp.setCodec(mapper); rval = jp.readValueAs(String.class); } else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) { jp.setCodec(mapper); rval = jp.readValueAs(Boolean.class); } else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT || initialToken == JsonToken.VALUE_NUMBER_INT) { jp.setCodec(mapper); rval = jp.readValueAs(Number.class); } else if (initialToken == JsonToken.VALUE_NULL) { rval = null; } return rval; }
Example 8
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializePetStoreFile() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(propertyDeserializer.propertyFromNode(jsonNode)); propertyDeserializer.deserialize(jsonParser, deserializationContext); ObjectNode obj = Mockito.mock(ObjectNode.class); Mockito.when(obj.get("name")).thenReturn(jsonNode); Mockito.when(obj.get("namespace")).thenReturn(jsonNode); Mockito.when(obj.get("prefix")).thenReturn(jsonNode); Mockito.when(obj.get("attribute")).thenReturn(jsonNode); Mockito.when(obj.get("wrapped")).thenReturn(jsonNode); Mockito.when(obj.get("xml")).thenReturn(obj); Assert.assertNotNull(propertyDeserializer.getXml(obj)); }
Example 9
Source File: JacksonResponse.java From nifi with Apache License 2.0 | 5 votes |
@Override public Object getEntity() { try { final JsonParser parser = jsonFactory.createParser(responseBody); parser.setCodec(codec); return parser.readValueAs(Object.class); } catch (final Exception e) { throw new RuntimeException("Failed to parse response", e); } }
Example 10
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 5 votes |
@Test public void testDeserializeArrays() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String array = "\"indicators\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"$ref\": \"#/definitions/Indicator\"\n" + " }\n" + " }"; JsonNode jsonNode = mapper.readTree(array); ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { propertyDeserializer.propertyFromNode(jsonNode); propertyDeserializer.deserialize(jsonParser, deserializationContext); propertyDeserializer.getXml(jsonNode); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example 11
Source File: HeaderDeserializerTest.java From java-jwt with MIT License | 5 votes |
@Test public void shouldNotRemoveKnownPublicClaimsFromTree() throws Exception { String headerJSON = "{\n" + " \"alg\": \"HS256\",\n" + " \"typ\": \"jws\",\n" + " \"cty\": \"content\",\n" + " \"kid\": \"key\",\n" + " \"roles\": \"admin\"\n" + "}"; StringReader reader = new StringReader(headerJSON); JsonParser jsonParser = new JsonFactory().createParser(reader); ObjectMapper mapper = new ObjectMapper(); jsonParser.setCodec(mapper); Header header = deserializer.deserialize(jsonParser, mapper.getDeserializationContext()); assertThat(header, is(notNullValue())); assertThat(header.getAlgorithm(), is("HS256")); assertThat(header.getType(), is("jws")); assertThat(header.getContentType(), is("content")); assertThat(header.getKeyId(), is("key")); assertThat(header.getHeaderClaim("roles").asString(), is("admin")); assertThat(header.getHeaderClaim("alg").asString(), is("HS256")); assertThat(header.getHeaderClaim("typ").asString(), is("jws")); assertThat(header.getHeaderClaim("cty").asString(), is("content")); assertThat(header.getHeaderClaim("kid").asString(), is("key")); }
Example 12
Source File: JSONReader.java From bigquery-etl-dataflow-sample with Apache License 2.0 | 5 votes |
/** * Reads an MusicBrainzDataObject from a json string. * * @param objectName - the namespace for the object * @param json the json string * @return */ public static MusicBrainzDataObject readObject(String objectName, String json) { MusicBrainzDataObject datum = new MusicBrainzDataObject(objectName); JsonFactory factory = new JsonFactory(); try { JsonParser parser = factory.createParser(json); parser.setCodec(new ObjectMapper()); while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token != null && token.equals(JsonToken.START_OBJECT)) { JsonNode jsonTree = parser.readValueAsTree(); jsonTree.fields().forEachRemaining(entry -> { if (entry.getValue() != null) { Object value = nodeValueToObject(entry.getValue()); if (value == null) { // NOOP - unknown node type } else { datum.addColumnValue(entry.getKey(), nodeValueToObject(entry.getValue())); } } else { logger.warn("null value for entry : " + entry.getKey()); } }); } } } catch (Exception e) { logger.error("parse exception", e); } return datum; }
Example 13
Source File: RealmsConfigurationLoader.java From keycloak with Apache License 2.0 | 5 votes |
private static JsonParser initParser(String file) { JsonParser p; try { JsonFactory f = new JsonFactory(); p = f.createParser(new File(file)); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); p.setCodec(mapper); } catch (Exception e) { throw new RuntimeException("Failed to parse file " + new File(file).getAbsolutePath(), e); } return p; }
Example 14
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializeArray() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String array = "{\n" + " \"properties\": {\n" + " \"type\": {\n" + " \"type\": \"string\",\n" + " \"default\": \"BBANDS\"\n" + " },\n" + " \"computeOn\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"type\": \"string\"\n" + " },\n" + " \"default\": [\n" + " \"close\"\n" + " ]\n" + " },\n" + " \"parameters\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"timeperiod\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 5\n" + " },\n" + " \"nbdevup\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 2\n" + " },\n" + " \"nbdevdn\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 2\n" + " },\n" + " \"matype\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 0\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; JsonNode jsonNode = mapper.readTree(array); ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(propertyDeserializer.propertyFromNode(jsonNode)); propertyDeserializer.deserialize(jsonParser, deserializationContext); propertyDeserializer.getXml(jsonNode); }
Example 15
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializePropertyFromNodeArray() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String arrayJson = "{\"parameters\": [\n" + "{\n" + " \"name\": \"petId\",\n" + " \"in\": \"path\",\n" + " \"description\": \"ID of pet to update\",\n" + " \"required\": true,\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + "},\n" + "{\n" + "\"name\": \"additionalMetadata\",\n" + "\"in\": \"formData\",\n" + "\"description\": \"Additional data to pass to server\",\n" + "\"required\": false,\n" + "\"type\": \"string\"\n" + "},\n" + "{\n" + "\"name\": \"file\",\n" + "\"in\": \"formData\",\n" + "\"description\": \"file to upload\",\n" + "\"required\": false,\n" + "\"type\": \"file\"\n" + "}\n" + "]}"; JsonNode root = mapper.readTree(arrayJson.getBytes()); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { propertyDeserializer.propertyFromNode(jsonNode); propertyDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example 16
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializeParameter() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Parameter.json"; String headerNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"header\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String queryNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"query\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String bodyNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"body\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String cookieNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"cookie\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String pathNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"path\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String formDataNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"formData\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; Map<String, String> stringMap = new HashMap<String, String>(); stringMap.put("Header", headerNode); stringMap.put("Body", bodyNode); stringMap.put("Path", pathNode); stringMap.put("FormData", formDataNode); stringMap.put("Cookie", cookieNode); stringMap.put("Query", queryNode); for (String nodeString : stringMap.values()) { JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(nodeString.getBytes()); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); ParameterDeserializer parameterDeserializer = new ParameterDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { parameterDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } } }
Example 17
Source File: OperatorSubCommandTest.java From besu with Apache License 2.0 | 4 votes |
private void runCmdAndCheckOutput( final Cmd cmd, final String configFile, final Path outputDirectoryPath, final String genesisFileName, final boolean generate, final Collection<String> expectedKeyFiles) throws IOException { final URL configFilePath = this.getClass().getResource(configFile); parseCommand( cmd( OperatorSubCommand.COMMAND_NAME, OperatorSubCommand.GENERATE_BLOCKCHAIN_CONFIG_SUBCOMMAND_NAME, "--config-file", configFilePath.getPath(), "--to", outputDirectoryPath.toString()) .args(cmd.argsArray()) .argsArray()); assertThat(commandErrorOutput.toString()).isEmpty(); final Path outputGenesisExpectedPath = outputDirectoryPath.resolve(genesisFileName); final File outputGenesisFile = new File(outputGenesisExpectedPath.toUri()); assertThat(outputGenesisFile).withFailMessage("Output genesis file must exist.").exists(); final String genesisString = contentOf(outputGenesisFile, UTF_8); final JsonObject genesisContent = new JsonObject(genesisString); assertThat(genesisContent.containsKey("extraData")).isTrue(); final Path expectedKeysPath = outputDirectoryPath.resolve("keys"); final File keysDirectory = new File(expectedKeysPath.toUri()); assertThat(keysDirectory).exists(); final File[] nodesKeysFolders = keysDirectory.listFiles(); assertThat(nodesKeysFolders).isNotNull(); if (generate) { final JsonFactory jsonFactory = new JsonFactory(); final JsonParser jp = jsonFactory.createParser(configFilePath); jp.setCodec(new ObjectMapper()); final JsonNode jsonNode = jp.readValueAsTree(); final int nodeCount = jsonNode.get("blockchain").get("nodes").get("count").asInt(); assertThat(nodeCount).isEqualTo(nodesKeysFolders.length); } final Stream<File> nodesKeysFoldersStream = stream(nodesKeysFolders); nodesKeysFoldersStream.forEach( nodeFolder -> assertThat(nodeFolder.list()).containsAll(expectedKeyFiles)); }
Example 18
Source File: JsonUtils.java From aws-xray-sdk-java with Apache License 2.0 | 3 votes |
/** * Parses given file for an array field and returns that array as a JSON node. * @param filePath - The path to the JSON file to parse. * @param fieldName - The name of the field in the JSON document to retrieve. Must be a field pointing to an array. * @return A node containing an array object, or null if the field cannot be found. * @throws IOException */ public static JsonNode getNodeFromJsonFile(String filePath, String fieldName) throws IOException { JsonParser jp = JSON_FACTORY.createParser(new File(filePath)); jp.setCodec(new ObjectMapper()); JsonNode jsonNode = jp.readValueAsTree(); return jsonNode.findValue(fieldName); }
Example 19
Source File: EsriJsonFactory.java From spatial-framework-for-hadoop with Apache License 2.0 | 2 votes |
/** * Construct an {@link com.esri.json.EsriFeature} from JSON * * @param JsonParser parser that is pointed at the root of the JSON file created by ArcGIS * @return EsriFeature instance that describes the fully parsed JSON representation * @throws JsonParseException * @throws IOException */ public static EsriFeature FeatureFromJson(JsonParser parser) throws JsonProcessingException, IOException{ parser.setCodec(jsonObjectMapper); return parser.readValueAs(EsriFeature.class); }
Example 20
Source File: EsriJsonFactory.java From spatial-framework-for-hadoop with Apache License 2.0 | 2 votes |
/** * Construct an {@link com.esri.json.EsriFeatureClass} from JSON * * @param JsonParser parser that is pointed at the root of the JSON file created by ArcGIS * @return EsriFeatureClass instance that describes the fully parsed JSON representation * @throws JsonParseException * @throws IOException */ public static EsriFeatureClass FeatureClassFromJson(JsonParser parser) throws JsonProcessingException, IOException{ parser.setCodec(jsonObjectMapper); return parser.readValueAs(EsriFeatureClass.class); }