Deserialize json file with dynamic fields into class with java - java

We have a JSON file providing us with data that needs to be loaded in memory, however I'm having a lot of trouble parsing the file as it does not use mapping of fields like I have seen classically:
{
"1er":{
"baccalaureat": [
{
"prog": "genie logiciel",
"limite": 30
},
{
"prog": "genie civil",
"limite": 15
},
{
"prog": "genie electrique",
"limite": 25
},
{
"prog": "Medecine",
"limite": 34
},
{
"prog": "actuariat",
"limite": 25
}
]
},
"2e":{
"maitrise": [
{
"prog": "informatique",
"limite": 25
}
]
},
"3e":{
"doctorat": [
{
"prog": "informatique",
"limite": 21
}
]
}
}
I use to deal with JSON files with "property" : "value" and I was hoping to be able to use jackson annotations. I've tried a test class shaped like this:
public class ClassOne {
#JsonProperty
public String Level;
#JsonProperty
public String[] programs;
}
But alas it does not working, throwing a mismatchedInputException. Of course, but I don't know how to map the properties, then:
private ObjectMapper objectMapper;
private ProgramLimitService programLimitService;
public ProgramLimitsLoader() {
objectMapper = new ObjectMapper();
}
private static List<String> values = new ArrayList<>();
public void load(String path) {
File file = new File(path);
ClassOne[] classes;
try {
classes = objectMapper.readValue(file, ClassOne[].class);
} catch (IOException e) {
throw new FileCantBeOpenException(e.getMessage());
}
}
I feel like it's because the structure of my class while I'm deserializing is wrong, but I can't put the finger on the problem.

Related

maping of JSON response using jackson [duplicate]

This question already has answers here:
Make Jackson interpret single JSON object as array with one element
(3 answers)
Closed 2 years ago.
I have a rest endpoint with the following response
{
"Shops": {
"Shop": [
{
"RowID": "1",
"Penalties": {
"Penalty": [
{
"PenaltyCode": "abc-01",
"PenaltyCount": "1"
},
{
"PenaltyCode": "abc-02",
"PenaltyCount": "2"
}
]
},
"VisitOutComes": {
"VisitOutCome": {
"TaskOutComeName": "text",
"TaskOutComeCount": "1"
}
}
}
]
}
}
I need to insert the response in a RDMS using hibernate.
I already done the hibernate part and the rest client part.
I am confused how to map the json to java classes.
I write the follwing classes , getter and setter are omitted for clarity.
public class Shops {
#JsonProperty("Shop")
private ArrayList<Shop> shop ;
public class Shop {
#JsonProperty("VisitOutComes")
private VisitOutComes visitOutComes ;
#JsonProperty("Penalties")
private Penalties penalties ;
#JsonProperty("RowID")
private String rowID ;
public class VisitOutComes {
#JsonProperty("VisitOutCome")
ArrayList<VisitOutCome> visitOutCome ;
public class Penalties {
#JsonProperty("Penalty")
ArrayList<Penalty> penalty ;
public class VisitOutCome {
#JsonProperty("TaskOutComeCount")
private String TaskOutComeCount;
#JsonProperty("TaskOutComeName")
private String TaskOutComeName ;
public class Penalty {
#JsonProperty("penaltyCode")
private String penaltyCode ;
#JsonProperty("penaltyCount")
private String penaltyCount ;
Then I wirte the main method as follows :
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
String path = "C:\\jars\\response.json" ;
Shops shops = mapper.readValue(new File(path),Shops.class);
Finally I get the error
Cannot deserialize instance of java.util.ArrayList jscksondemo.VisitOutCome out of START_OBJECT token
I appreciate any help on how to create the maping for this json file.
Try with this JSON:
{
"Shops": {
"Shop": [
{
"RowID": "1",
"Penalties": {
"Penalty": [
{
"PenaltyCode": "abc-01",
"PenaltyCount": "1"
},
{
"PenaltyCode": "abc-02",
"PenaltyCount": "2"
}
]
},
"VisitOutComes": {
"VisitOutCome": [
{
"TaskOutComeName": "text",
"TaskOutComeCount": "1"
}
]
}
}
]
}
}
I think the bracket of your VisitOutCome List is not right.

Deserializing JSON with special characters into a string

I'm trying to parse a json file that looks like this
{
"foo": "v2",
"bar": [
"abc/bcf<object#twenty>.xyz",
"abc/fgh<object#thirtu>.xyz"
]
}
The code I have is currently this:
Config.java
private static final ObjectMapper OBJECT_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
OBJECT_MAPPER.enableDefaultTyping();
}
#JsonCreator
public Config(
#JsonProperty(value = "foo", required = true) final String version,
#JsonProperty(value = "bar") final List<String> barTypes) {
// rest of constructor
}
public static Config fromJson(final Reader reader)
throws IOException {
return OBJECT_MAPPER.readValue(reader, Config.class);
}
I am getting an error:
Failed to parse type 'abc/bcf<object#twenty>.xyz' (remaining: '<object#twenty>.xyz'): Cannot locate class 'abc/bcf', problem: abc/bcf"
Is there something special I need to do in order to read "<" as String?
I'm reading the file into a BufferReader with StandardCharsets.UTF_8 like this:
try (BufferedReader reader = Files.newBufferedReader(configFile.toPath(), StandardCharsets.UTF_8)) {
config = Config.fromJson(reader);
}
Edit: I actually do need defaultTyping for an ArrayList that has polymorphic types:
"Vehicles": [
"java.util.ArrayList",
[
{
"name": "Car"
},
{
"name": "Train"
}
]
I currently use a MixIn for declaring the subtypes. However, this stops working if I remove DefaultTyping.
Remove OBJECT_MAPPER.enableDefaultTyping(); and it will work fine. This method is anyway deprecated.
In case you want to read automatic polymorphic types, use activateDefaultTyping(PolymorphicTypeValidator ptv).

How to convert list of filepaths to a JSON object in Java

I've a requirement to convert a set of file structure obtained from DB into a JSON.
For example:
From DB, I get the following path and file attributes:
Record 1:
"path": "/sd/card/camera/pics/selfie.jpg"
"fileName": "selfie.jpg",
"mimeType": "image/jpeg"
Record 2:
"path": "/sd/card/camera/pics/personal/selfie1.jpg"
"fileName": "selfie1.jpg",
"mimeType": "image/jpeg"
and so on..
I need to convert this to a JSON like:
[{
"sd": [{
"card": [{
"camera": [{
"pics": [{
"fileName": "selfie.jpg",
"path": "/sd/card/camera/pics/selfie.jpg",
"mimeType": "image/jpeg"
},
{
"personal": [{
"fileName": "selfie1.jpg",
"path": "/sd/card/camera/pics/personal/selfie1.jpg",
"mimeType": "image/jpeg"
}]
}
]
}]
}]
}]
}]
I'm going to give you a 'jackson' solution.
First, build an object (or more, I let you deal with the Java inheritance, or use any kind of structure you want to use).
Like this one by example :
#JsonSerialize(using = CustomSerializer.class)
public class Something {
private String currentFolder; // Name of the folder if this instance of something is a folder
private Something[] childs;
private Map<String,String> currentPicture; // Picture properties if this instance of something is a picture
public Something() {currentPicture = new HashMap<String,String>();}
public Something[] getChilds() {
return childs;
}
public void setContent(Something[] _childs) {this.childs = _childs;}
public String getCurrentFolder() {return currentFolder;}
public void setCurrentFolder(String _currentFolder) {this.currentFolder = _currentFolder;}
public Map<String,String> getCurrentPicture() {return currentPicture;}
public void setCurrentPicture(Map<String,String> currentPicture) {this.currentPicture = currentPicture;}
}
Then, create the CustomSerializer, that will help you do whatever you want to do :
public class CustomSerializer extends JsonSerializer<Something>{
#Override
public void serialize(Something value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
// Adding the folder into the json, only if it exists
if(value.getCurrentFolder()!=null){
jgen.writeObjectField(value.getCurrentFolder(), value.getChilds());
}
// Adding properties of the picture, only if they exist
if(value.getCurrentPicture()!= null){
for(String k : value.getCurrentPicture().keySet()){
jgen.writeObjectField(k,value.getCurrentPicture().get(k));
}
}
jgen.writeEndObject();
}
}
Finally (I've not done this one, but you'll do it I'm sure !) create a mapper from what you read to the "Something" class.
I build the object manually here (quickly, so it's not clean):
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Something s = new Something();
s.setCurrentFolder("toto");
Something s2 = new Something();
s2.setCurrentFolder("tata");
Something s2bis = new Something();
s2bis.setCurrentFolder("tataBis");
Something[] s2Group = {s2bis};
s2.setContent(s2Group);
Something s2bispic = new Something();
s2bispic.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfILikeSoMuch.jpg");
s2bispic.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg");
s2bispic.getCurrentPicture().put("mimeType", "image/jpeg");
Something s2bispic2 = new Something();
s2bispic2.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfIDontLike.jpg");
s2bispic2.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg");
s2bispic2.getCurrentPicture().put("mimeType", "image/jpeg");
Something[] s2BisGroup = {s2bispic,s2bispic2};
s2bis.setContent(s2BisGroup);
Something s3 = new Something();
s3.getCurrentPicture().put("fileName", "selfie.jpg");
s3.getCurrentPicture().put("path", "toto/selfie.jpg");
s3.getCurrentPicture().put("mimeType", "image/jpeg");
Something[] sGroup = {s2,s3};
s.setContent(sGroup);
ObjectMapper mapper = new ObjectMapper();
String temp = mapper.writeValueAsString(s);
System.out.println(temp);
}
And this is what I get :
{
"toto":[
{
"tata":[
{
"tataBis":[
{
"path":"toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg",
"fileName":"ThatPictureOfMysSelfILikeSoMuch.jpg",
"mimeType":"image/jpeg"
},
{
"path":"toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg",
"fileName":"ThatPictureOfMysSelfIDontLike.jpg",
"mimeType":"image/jpeg"
}
]
}
]
},
{
"path":"toto/selfie.jpg",
"fileName":"selfie.jpg",
"mimeType":"image/jpeg"
}
]
}
Regards,

How to parse JSON Response with GSON (Java / Android)

I just need a quick advice, as i am a total beginner with JSON.
I get the following response from a webserver, which i store in a String:
{
"station62":[
{
"departureTime":1982,
"delay":"-1.0",
"line":"6",
"stationName":"randomname",
"direction":2
}
],
"station63":[
{
"departureTime":1234,
"delay":"-1.0",
"line":"87",
"stationName":"anotherrandomname",
"direction":2
}
],
"station64":[
{
"departureTime":4542,
"delay":"-1.0",
"line":"4",
"stationName":"yetanotherrandomname",
"direction":2
}
],
"station65":[
{
"departureTime":1232,
"delay":"-1.0",
"line":"23",
"stationName":"onemorerandomname",
"direction":2
}
]
}
(Sorry, i dont know how the indent works on here.)
The response is longer, but for this example it is shortened. So what i need is to parse the information of each of these "station"-objects.
I dont need the "station62"-String, i only need "departureTime", "delay", "line", "stationName" and "direction" in a java-object.
I have read this, but i couldnt make it work: https://stackoverflow.com/a/16378782
I am a total beginner, so any help would be really appreciated.
Edit: Here is my code:
I made a wrapper class just like in the example link above. I played with the map types a bit, but no luck so far.
public class ServerResponse
{
private Map<String, ArrayList<Station>> stationsInResponse = new HashMap<String, ArrayList<Station>>();
public Map<String, ArrayList<Station>> getStationsInResponse()
{
return stationsInResponse;
}
public void setStationsInResponse(Map<String, ArrayList<Station>> stationsInResponse)
{
this.stationsInResponse = stationsInResponse;
}
}
The problem is, that this map does not get filled by the gson.fromJSON(...)-call i am showing below. The map size is always zero.
Station class looks like this:
public class Station
{
String line;
String stationName;
String departureTime;
String direction;
String delay;
// getters and setters are there aswell
}
And what i am trying to do is
Gson gson = new Gson();
ServerResponse response = gson.fromJson(jsonString, ServerResponse.class);
where "jsonString" contains the JSON response as a string.
I hope that code shows what i need to do, it should be pretty simple but i am just not good enough in JSON.
EDIT 2
Would i need my JSON to be like this?
{"stationsInResponse": {
"station62": [{
"departureTime": 1922,
"delay": "-1.0",
"line": "8",
"stationName": "whateverrandomname",
"direction": 2
}],
"station67": [{
"departureTime": 1573,
"delay": "-1.0",
"line": "8",
"stationName": "rndmname",
"direction": 2
}],
"station157": [{
"departureTime": 1842,
"delay": "-2.0",
"line": "8",
"stationName": "randomname",
"direction": 2
}]
}}
Here is the working code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
public class GSONTest {
public static void main(String[] args){
String gsonStr = "{\"stationsInResponse\": { \"station62\":[ { \"departureTime\":1982,\"delay\":\"-1.0\",\"line\":\"6\",\"stationName\":\"randomname\",\"direction\":2} ],\"station63\":[ { \"departureTime\":1981,\"delay\":\"-1.1\",\"line\":\"7\",\"stationName\":\"randomname2\",\"direction\":3} ]}}";
Gson gson = new Gson();
Response response = gson.fromJson(gsonStr, Response.class);
System.out.println("Map size:"+response.getStationsInResponse().size());
for (Iterator iterator = response.getStationsInResponse().keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
ArrayList<Station> stationList = (ArrayList<Station>) response.getStationsInResponse().get(key);
for (Iterator iterator2 = stationList.iterator(); iterator2.hasNext();) {
Station station = (Station) iterator2.next();
System.out.println("Delay: "+station.getDelay());
System.out.println("DepartureTime: "+station.getDepartureTime());
System.out.println("Line: "+station.getLine());
System.out.println("StationName: "+station.getStationName());
}
}
}
}
class Response {
private Map<String, List<Station>> stationsInResponse;
//getters and setters
public Map<String, List<Station>> getStationsInResponse() {
return stationsInResponse;
}
public void setStationsInResponse(Map<String, List<Station>> stationsInResponse) {
this.stationsInResponse = stationsInResponse;
}
}
class Station {
private String departureTime;
public String getDepartureTime() {
return departureTime;
}
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}
public String getDelay() {
return delay;
}
public void setDelay(String delay) {
this.delay = delay;
}
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
private String delay;
private String line;
private String stationName;
private String direction;
}
Output in console is like this(as I shortened your json string):
Map size:2
Delay: -1.0
DepartureTime: 1982
Line: 6
StationName: randomname
Delay: -1.1
DepartureTime: 1981
Line: 7
StationName: randomname2
First I'll point out your mistake, then I'll give you the solution.
The structure you're asking for in your deserialization code looks like this:
{
"stationsInResponse": {
"station1": [
{
"name": "Station 1"
}
],
"station2": [
{
"name": "Station 2"
}
]
}
}
Solution
The deserialization code you really need to deserialize the structure you're getting as input, is as follows:
Gson gson = new Gson();
Type rootType = new TypeToken<Map<String, List<Station>>>(){}.getType();
Map<String, List<Station>> stationsMap = gson.fromJson(json, rootType);
This is because a JSON object, whose properties are unknown at compile-time, which your root object is, maps to a Java Map<String, ?>. The ? represent the expected Java type of the JSON object value, which in your case is either List<Station> or Station[], whichever you prefer.
If you wanted to, you could combine all the stations in the map into one List of stations like so:
List<Station> stations = new ArrayList<>(stationsMap.size());
for (List<Station> stationsList : stationsMap.values()) {
for (Station station : stationsList) {
stations.add(station);
}
}

GSON identifying JSON Object as Primitive

I am writing a relatively simple messaging app that saves its logs in the JSON format, and I am using the GSON library to parse these. I load a JSON file from a server, and put it trough Gson.toJsonTree() function. I'm not sure this is expected, but when I test the result from the previous function with the isJsonSomething() functions (isJsonObject,isJsonAray,isJsonNull,isJsonPrimitive), isJsonPrimitive returns true, and I can't parse it into a object. This is my JSON file's contents:
{
"users": [
{
"picture": "",
"type": "user",
"name": "kroltan"
}
],
"description": "No description",
"messages": [
{
"content": "something",
"time": "2013-08-30 00:38:17.212000",
"type": "message",
"author": "someone"
}
],
"type": "channel",
"name": "default"
}
And here is the class used to parse it into POJOs: (CLEANUP comments is where I've removed irrelevant code from the post)
package com.example.testapp;
//CLEANUP: All needed imports
import com.example.testapp.data.*;
import com.google.gson.*;
public class JSONConverter {
public interface JsonTypeLoadedListener {
public void onSucess(JSONType jsonType);
public void onFailure(Exception e);
}
public static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss.SSS";
public static final HashMap<String, Class<?>> JSON_TYPES = new HashMap<String, Class<?>>();
public JSONConverter() {
JSON_TYPES.clear();
JSON_TYPES.put("channel", Channel.class);
JSON_TYPES.put("user", User.class);
JSON_TYPES.put("message", Message.class);
}
public void loadFromURL(final URL url, final JsonTypeLoadedListener listener) {
new Thread(new Runnable() {
#Override
public void run() {
JsonObject result = null;
Gson gson = new GsonBuilder().setDateFormat(DATE_FORMAT).create();
if (url.getProtocol().equals("http")) {
try {
String content = //Loads from a server, omitted for clarity
result = gson.toJsonTree(content).getAsJsonObject();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
listener.onFailure(e);
return;
}
} else if (url.getProtocol().equals("file")) {
try {
String content = //Loads from a file, omitted for clarity
result = gson.toJsonTree(content).getAsJsonObject();
br.close();
} catch (Exception e) {
e.printStackTrace();
listener.onFailure(e);
return;
}
}
listener.onSucess((JSONType) gson.fromJson(result, JSON_TYPES.get(result.get("type").getAsString())));
}
}, "URLLoader").start();
}
public JSONType loadFromString(String s) {
Gson gson = new Gson();
JsonObject result = gson.toJsonTree(s).getAsJsonObject();
return (JSONType) gson.fromJson(result, JSON_TYPES.get(result.get("type").getAsString()));
}
}
The classes Message, User and Channel all inherit from JSONType (a custom class with a field called type and some utility methods) and contain all values present in the above mentioned JSON file.
When it reaches gson.toJsonTree(content).getAsJsonObject(), I get this error in Logcat (string omitted for clarity, it's just the full file):
java.lang.IllegalStateException: Not a JSON Object: "String containing all the file with tabs represented as \t"
I'm guessing that the tabs are causing your issue. Try to remove them with:
content = content.replaceAll("\\s","")
this will simply clean your json string from any whitespace.
Btw I suggests you to get rid of Gson library and use directly the JSONObject provided in the android sdk. You can initialize it directly with the json string, as new JSONObject(content). :)

Categories