In Jackson 2.4, you can convert as follows:
MyClass newJsonNode = jsonObjectMapper.treeToValue(someJsonNode, MyClass.class);
where jsonObjectMapper is a Jackson ObjectMapper.
In older versions of Jackson, it would be
MyClass newJsonNode = jsonObjectMapper.readValue(someJsonNode, MyClass.class);
Answer from icedtrees on Stack OverflowIn Jackson 2.4, you can convert as follows:
MyClass newJsonNode = jsonObjectMapper.treeToValue(someJsonNode, MyClass.class);
where jsonObjectMapper is a Jackson ObjectMapper.
In older versions of Jackson, it would be
MyClass newJsonNode = jsonObjectMapper.readValue(someJsonNode, MyClass.class);
This should do the trick:
mapper.readValue(fileReader, MyClass.class);
I say should because I'm using that with a String, not a BufferedReader but it should still work.
Here's my code:
String inputString = // I grab my string here
MySessionClass sessionObject;
try {
ObjectMapper objectMapper = new ObjectMapper();
sessionObject = objectMapper.readValue(inputString, MySessionClass.class);
Here's the official documentation for that call: http://jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/map/ObjectMapper.html#readValue(java.lang.String, java.lang.Class)
You can also define a custom deserializer when you instantiate the ObjectMapper:
http://wiki.fasterxml.com/JacksonHowToCustomDeserializers
Edit:
I just remembered something else. If your object coming in has more properties than the POJO has and you just want to ignore the extras you'll want to set this:
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Or you'll get an error that it can't find the property to set into.
java - Convert JsonNode into Object - Stack Overflow
Does it consider a bad practice to use jsonNode as the return type
java - How to parse a JSON string into JsonNode in Jackson? - Stack Overflow
How to parse Json array with 2 or more different types using Jackson?
http://www.baeldung.com/jackson-inheritance
You'll need to have those types inherit from one base class though.
Also; that is really bad JSON.
More on reddit.comI am a beginner of Java & Springboot. I feel like I am doing something that does not use the full strength of spring boot, e.g. using JsonNode & return untyped Response Entity etc.
Just wonder if there is any best practice when dealing with this kind of situation, when I want to return an object as part of result.
In Typescript I can create an interface for the object, how about in java / Springboot?
TestService.java
...
@Service
@Slf4j
public class TestService {
private final TsKvRepository tsKvRepository;
private final ObjectMapper objectMapper = new ObjectMapper();
@Autowired
public TestService(TsKvRepository tsKvRepository) {
this.tsKvRepository = tsKvRepository;
}
public DeferredResult<ResponseEntity> getLatestTimeseries(String entityIdStr, String eui64) {
DeferredResult<ResponseEntity> result = new DeferredResult<>();
try {
UUID entityId = UUID.fromString(entityIdStr);
List<TsKvEntity> tsList = this.tsKvRepository.findLatestByEui64(entityId, eui64);
if (!tsList.isEmpty()) {
TsKvEntity tsKvEntity = tsList.get(0);
JsonNode jsonNode = objectMapper.readTree(tsKvEntity.getJsonValue());
ResponseEntity<?> responseEntity = new ResponseEntity<>(jsonNode.get(0), HttpStatus.OK);
result.setResult(responseEntity);
} else {
ResponseEntity<?> responseEntity = new ResponseEntity<>("No data found", HttpStatus.NOT_FOUND);
result.setResult(responseEntity);
}
}catch(IllegalArgumentException | NullPointerException e) {
ResponseEntity<?> responseEntity = new ResponseEntity<>("Invalid UUID format or null keysStr: " + e.getMessage(), HttpStatus.BAD_REQUEST);
result.setResult(responseEntity);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return result;
}
}A slight variation on Richards answer but readTree can take a string so you can simplify it to:
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}");
You need to use an ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead
JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}");
JsonNode actualObj = mapper.readTree(jp);
Further documentation about creating parsers can be found here.