Jackson library 1.x array if one element sometimes just a string - java

I have to deal with strange Json messages.
there are Arrays in the schema, but if there are only one element array becomes an string.
So sometimes it is:
"Cisco-AVPair": [
"connect-progress=Call Up",
"nas-tx-speed=8083000",
"nas-rx-speed=8083000"
],
and sometimes:
"Cisco-AVPair": "connect-progress=Call Up".
How to overcome this if I use Jackson 1.8.2
I am afraid I am not in control of source code generation and only can parse it.
I do parse it with:
mapper.readValue(json, refType);
while my type reference is:
#JsonProperty("Cisco-AVPair")
private List<String> CiscoAVPair = new ArrayList<String>();
#JsonProperty("Cisco-AVPair")
public List<String> getCiscoAVPair() {
return CiscoAVPair;
}
#JsonProperty("Cisco-AVPair")
public void setCiscoAVPair(List<String> CiscoAVPair) {
this.CiscoAVPair = CiscoAVPair;
}
As you see it is list of strings, but sometimes comes just as a string.

There's a specific config option even in ancient Jackson 1.8.2 that accomplishes exactly what you need.
You should configure your ObjectMapper instance to always deserialize JSON values as a List, no matter whether values come as an array or as a single element. Please see javadocs here for the deserialization feature you need to enable, and these other javadocs to see how to actually activate/deactivate a feature on an ObjectMapper instance.
ObjectMapper mapper = ...;
mapper = mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Bear in mind that configure() method returns another instance of ObjectMapper.

Related

How to Parse untyped, nested JSON with Polymorphism?

I am using Feign to hit external APIs, and then to process the externally generated JSON (aka, the response data cannot be modified in any way), and I am trying to bundle these together into an extensible super type. At this point, I am not even sure if what I am trying to do is possible with Jackson / Feign. If it would be much easier to abandon (or heavily restructure) the polymorphism, I think I am also ready to give up on it and just create a bunch of sub classes.
Here are my two main questions, with more context below.
Should I just separate the easily deduced types from the complex types, and have a little more duplicated boiler plate?
How can I create a custom deserializer for the list object I linked? Ideally I would like to have some way to populate the more boiler plate fields less manually -- as an example, it would be great if I could call default deserializers inside it, which would rely more on the standard annotations in other objects.
Ideally, I would like one class, like this:
public final class BillApiResponse {
#Valid
#JsonProperty("response_status")
private boolean responseStatus;
#Valid
#JsonProperty("response_message")
private String responseMessage;
#JsonProperty("response_data")
private BillApiResponseData responseData;
//getters and setters, etc.
}
and then I would to have Jackson automatically map the simpler objects in whatever way is easiest (LoginResponse, LoginError), while I would try to implement a custom handler for the more complex objects (UpdateObject, ListOfObjects).
So, something like this:
#JsonTypeInfo(use = Id.DEDUCTION)
#JsonSubTypes({
#Type(value = BillLoginSuccess.class),
#Type(value = BillErrorResponse.class),
//#Type(value = BillResponseObject[].class) <--- This breaks things when added
})
// #JsonTypeResolver(value = BillResponseTypeResolver.class) <--- Open to using one of
// these if I can figure out how
// #JsonDeserialize(using = BillResponseDeserializer.class) <--- Also open to using a
// custom deserializer, but I would like to keep it only for certain parts
public interface BillApiResponseData {}
Here is a link to the API specification I am trying to hit:
Get a List of Objects
This returns an untyped array of untyped objects. Jackson does not seem to like that the array is untyped, and stops parsing everything there. Once inside, we would have to grab the type from a property.
{
"response_status" : 0,
"response_message" : "Success",
"response_data" : [{
"entity" : "SentPay",
"id" : "stp01AUXGYKCBGFMaqlc"
// More fields
} // More values]
}
Login
This returns a totally new object. Generally not having issues handling this one (until I add support for the above list, and then all of the parsing breaks down as Jackson throws errors).
Update Object
This returns an untyped object. Once again, we would have to go inside and look at the property.
I have tried a number of things, but generally I was not successful (hence I am here!).
These include:
Trying to hook into the lifecycle and take over if I detect an array object. I believe this fails because Jackson throws an error when it sees the array does not have a type associated with it.
SimpleModule customDeserializerModule = new SimpleModule()
.setDeserializerModifier(new BeanDeserializerModifier() {
#Override
public JsonDeserializer<?> modifyDeserializer(
DeserializationConfig config,
BeanDescription beanDesc,
JsonDeserializer<?> defaultDeserializer) {
if (beanDesc.getBeanClass().isArray()) {
return new BillResponseDeserializer(defaultDeserializer);
} else {
return defaultDeserializer;
}
}
});
Custom Deserializers. The issue I have is that it seems to want to route ALL of my deserialization calls into the custom one, and I don't want to have to handle the simpler items, which can be deduced.
TypeIdResolvers / TypeResolvers. Frankly these are confusing me a little bit, and I cannot find a good example online to try out.

WebClient does not return a "valid" list of Strings

I have a spring boot app that among others, has an endpoint that when hit, returns a list of strings. I also have another spring boot app that hits the first app's endpoint to get the data. The fetch code:
return webClient.get().uri("/sensors/get-cities").headers(httpHeaders -> {
httpHeaders.set("Authorization", auth);
}).retrieve()
.bodyToFlux(String.class).collectList().block();
The above yields a list but with this format when I inspect it in the debbuger, "["city"]". The outer double quotes, I get them because it's a string but the brackets and the inside double quotes, I do not. I tried replacing these characters but I had no luck with the brackets (tried regex). It is like they are not there, but at the same time they are. I am confused at this point. But I think that the behavior of the fetch code is not normal, it should yield a valid array of strings.
What you are probably getting (im guessing here) is a response body that looks something like this:
[
"New York",
"Madrid",
"London"
]
You then tell webflux that you want to convert the body to a Flux of String by calling bodyToFlux(String.class).
So the framework takes the entire response and makes a string out of it
// A string of the entire array (im escaping the quotation marks)
"[\"New York\",\"Madrid\",\"London\"]"
And then the framework will throw the entire thing into a Flux which means it takes the first position in the Flux. You then emit all the values into a List by calling collectList The equivalent code is sort of:
List<String> oneString = Flux.just("[\"New York\",\"Madrid\",\"London\"]")
.collectList()
.block();
So you get a list, with one string in it, which is the entire body.
What you probably want to do is to get a list out if it. And this is one way to do it:
List<String> strings = webClient.get()
.uri("/sensors/get-cities")
.headers(httpHeaders -> {
httpHeaders.set("Authorization", auth);
})
.retrieve()
.bodyToMono(new ParameterizedTypeReference<List<String>>() {})
.block();
Spring explains ParameterizedTypeReference:
The purpose of this class is to enable capturing and passing a generic Type. In order to capture the generic type and retain it at runtime
So its sort of a class that makes sure we can use generic types like List<T> and helps us with type information.
So what we do is that we now take the response and tell the framework that the body is a list of strings directly. We dont need to do collectList anymore as the framework will stick it in a list for us. We then call block to wait in the response.
Your Springboot API returns result as parsed to JSON (this is default behavior). So it first builds a list of Strings (in your case just a single String "city" and than serializes it to Json. In this case since it is a list it serializes it to JSON array as opposed to JSON Object. Read about JSON here. So in your second Springboot app that hits the API from the first one should assume that you are getting JSON which you need to parse to get your list. To parse it you can use readValue() method of ObjectMapper class of Json Jackson library which is a default JSON library in Springboot. your code would be
List<String> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}
In addition I wrote my own Open-source library called MgntUtils, that includes JsonUtils class which is a thin wrapper over Json Jackson library. It provides just Json parser and serializer, but in many cases that is all you need. With my library you would only need one dependency as oppose to Jackson, and JsonUtils class just have 4 methods, so by far easier to understand. But in your case if you use my library the code would be very similar to the above code. It would be something like this:
List<String> myList;
try {
myList = JsonUtils.readObjectFromJsonString(myJsonString, List.class);
} catch(IOException ioe) {
...
}
Note that in this case you won't have to instantiate and configure ObjectMapper instance as readObjectFromJsonString is a static method. Anyway if you are interested in using my library you can find maven artifacts here and The library itself with source code and javadoc is on Github here. Javadoc for JsonUtils class is here

How to deserialize this json

This is the json response returned by MediaWiki API. I want to create a class to be able to deserialize it to it use Jackson library. The problem is that this json contains a key which is different from each request (here is 290).
{
"query-continue": {
"revisions": {
"rvcontinue": 633308090
}
},
"query": {
"pages": {
"290": {
"pageid": 290,
"ns": 0,
"title": "A",
"revisions": [
{
"user": "Mr. Guye",
"timestamp": "2014-12-07T17:45:55Z",
"comment": "comment",
"contentformat": "text/x-wiki",
"contentmodel": "wikitext",
"*": "content"
}
]
}
}
}
}
How could create a class (or configure the mapper) to be able to deserialize this json?
You can deserialize JSON to multiple formats using Jackson. One way that you mentioned is to convert the JSON to a POJO which may be difficult when the keys are dynamic. Another approach is to deserialize the JSON to the Jackson Tree Model which is called JsonNode. The following illustrates how you can parse the provided JSON to a JsonNode and then retrieve the various attributes.
final ObjectMapper mapper = new ObjectMapper();
// Parse the JSON, deserialize to the Tree Model
final JsonNode jsonNode = mapper.readTree(jsonString);
// Get hold of the "query -> pages" node.
final JsonNode pages = jsonNode.path("query").path("pages");
// Iterate the pages
for (final JsonNode page : pages) {
// Work with the page object here...
System.out.println(page.get("pageid")); // -> 290
}
The JsonNode object is very flexible and contains various convenience functions for accessing the data. As shown in the example above the path() and get() methods are two ways of accessing the data. If you use get() the property MUST exist, if you use path the property MAY exist. Furthermore, there are multiple ways of iterating the sub-elements and the loop shown above is one way.
Take a look at the Jackson docs for more info.
The short answer is you can't, at least not in the current format with that abominable asterisk being present. Therefore, we will have to employ a bit of hackery here to get the job done, and I warn you upfront, it's not going to be pretty.
Firstly, copy that response, then go to http://www.jsonschema2pojo.org/ and paste it into the JSON textbox. After pasting it, change the asterisk to something more civilized, like "content". Select JSON (default is JSON Schema) for the Source Type, input your package and root class name respectively, and click JAR to generate the package with all the POJO's that map to this JSON. You could also click "Preview" and copy paste the code into your source files -- it's really up to you.
Now that we have a valid version of this JSON structure, we use Jackson to read it in. If your JSON String is called jsonResponse and the corresponding POJO class is MediaWiki, then you convert it with Jackson like this:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MediaWiki mw = objectMapper.readValue(profileJson, MediaWiki.class);
The key here is the FAIL_ON_UNKNOWN_PROPERTIES being set to false, which means it will ignore that asterisk, and create everything else for you.
Now, to actually grab whatever value was present for that asterisk and store it into our "content" attribute (or whatever else you wanted replace the asterisk with), you are going to have to parse this sucker out client-side and pass it as a separate input parameter, and to do that, you will have to yank it out by calling something like this:
var content = query.pages.290.revisions["*"];
This content parameter is passed and stored it into your POJO's content attribute.
I know it's a lot of work, and if anyone else has a more elegant solution, please share. As I said, mine was not going to be pretty. :-)
This looks like key value pair.
You can use map in order to deserialize key value pairs:
public class Query {
private Map<Integer, Page> pages;
public Map<Integer, Page> getPages() {
return pages;
}
public void setPages(Map<Integer, Page> pages) {
this.pages = pages;
}
}
Jackson handles such deserialization by default.

Convert Json to a java object

What is the way to generate a Java object with get and set methods?
You should write a java bean with properties maching the JSON key's, from that point since you already have a reader its a simple as
YourObject obj = gson.fromJson(br, YourObject.class);
UPDATE
With respect to your comment, when you don't want or can't create a bean it usually boils down to parsing JSON to map. GSON (afaik) doesn't have a built-in for this, but its not hard to build a method that will traverse GSON's objects. You have an example in this blog
http://itsmyviewofthings.blogspot.it/2013/04/jsonconverter-code-that-converts-json.html
As you seem to be open to alternatives, take a look at Jackson as well (the two libs are the de-facto standard in JAVA).
With jackson you don't have to create a bean to support deserialization, e.g.
String json = "{\"id\":\"masterslave\"}";
Map<String,String> map = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
//convert JSON string to Map
map = mapper.readValue(json,
new TypeReference<HashMap<String,String>>(){});
http://www.jsonschema2pojo.org/
That link helps generate the Java object format based on the GSON you feed in. Just make sure you set the settings exactly as you need it. As always, it's not a good idea to just copy-paste generated code, but it might be of help.

From org.json JSONObject to org.codehaus.jackson

I want to move from org.json to org.codehaus.jackson. How do I convert the following Java code?
private JSONObject myJsonMessage(String message){
JSONObject obj = new JSONObject();
obj.put("message",message);
return obj;
}
I left out the try-catch block for simplicity.
Instead of JSONObject use Jackson's ObjectMapper and ObjectNode:
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("message", "text");
This would be Jackson's equivalent of your current org.json code.
However, where Jackson really excels is in its capacity to do complex mappings between your Java classes (POJOs) and their JSON representation, as well as its streaming API which allows you to do really fast serialization, at least when compared with org.json's counterparts.
There is no JSONObject in Jackson api. Rather than returning a JSONObject, you can either return a Map or a Java Bean with message property that has getters and setters for it.
public class MyMessage {
private String message;
public void setMessage(final String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}
So, your method will be reduced to:
private MyMessage(String message) {
MyMessage myMessage = new MyMessage();
myMessage.setMessage(message);
return myMessage;
}
Another aspect of this change would be changing the serialization code, to convert MyMessage back to json string. Jackson does Java Beans, maps by default, you don't need to create a JSONObject e.g.,
private String serializeMessage(MyMessage message){
//Note: you probably want to put it in a global converter and share the object mapper
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(message);
}
The above will return {message: "some message"}
I have skipped the exceptions for brevity.
If you want to upgrade from org.json library to Jackson piece by piece, and initially retaining same API, you might want to read "Upgrade from org.json to Jackson".
This would at least make your code about 3x faster for basic JSON reading and writing; plus you could -- if you so choose -- start converting processing, as Jackson makes it easy to convert between Trees and POJOs (ObjectMapper.treeToValue(...), valueToTree, convertValue between POJOs etc. etc).
Just keep in mind that tools that you are familiar with may bias your thinking to certain patterns, and keeping an open mind can help you find even better ones.
In case of Jackson (or GSON or other mature Java JSON tools), you really should consider where proper data-binding could help, instead of using JSON-centered tree model (that org.json offers). Tree Models keep your thinking grounded to JSON structure, which is sometimes useful; but might also prevent you from seeing more natural patterns that come from defining POJO structure to reflect expected JSON, and operating on Java Objects directly.

Categories