JSON validation returns different results - java

I have created a minimal application in order to debug the following problem:
The application is creating a Java object which includes a GeoJsonPoint. When the object gets validated against an automatically created json schema, I receive different results when starting the application multiple times. For example, I am starting the application for 5 times and the result is "Json not valid!". When I start the application another time, I receive the result "Json successfully validated".
The error message when the json is not valid tells me:
/geoPosition/coordinates: instance type (array) does not match any allowed primitive type (allowed: [object])
The following line is randomly returning a different json schema:
JsonNode fstabSchema = schemaFactory.createSchema(inputObj.getClass());
I do not understand that this happens randomly. Has someone seen this behavior before?
To exclude dependency problems during runtime, I have created a jar with all dependencies (jar-with-dependencies)
Below are my files:
pom.xml dependencies:
<dependencies>
<dependency>
<groupId>com.github.reinert</groupId>
<artifactId>jjschema</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
App.java:
package de.s2.json.test;
import java.util.ArrayList;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
public class App
{
public static void main( String[] args )
{
Address address = new Address();
address.setCountry("Deutschland");
GeoJsonPoint geoPoint = new GeoJsonPoint(12, 23);
address.setGeoPosition(geoPoint);
ArrayList<String> ret = null;
try {
ret = Toolbox.validateJson(address);
} catch (ProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ret != null) {
System.out.println("Json not valid!");
for (int i = 0; i < ret.size(); i++) {
System.out.println(ret.get(i));
}
} else {
System.out.println("Json successfully validated");
}
}
}
Toolbox.java:
package de.s2.json.test;
import java.util.ArrayList;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
public class Toolbox {
public static <T> ArrayList<String> validateJson(T inputObj) throws ProcessingException {
com.github.reinert.jjschema.v1.JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode fstabSchema = schemaFactory.createSchema(inputObj.getClass()); // <= here I get different results
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(fstabSchema);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode baseReceiptJson = objectMapper.convertValue(inputObj, JsonNode.class);
ProcessingReport report;
report = schema.validate(baseReceiptJson);
ArrayList<String> validationErrorDetails = new ArrayList<String>();
if (!report.isSuccess()) {
StringBuilder builder = new StringBuilder();
builder.append("Not all required fields are filled with data");
builder.append(System.getProperty("line.separator"));
for (Iterator<ProcessingMessage> i = report.iterator(); i.hasNext();) {
ProcessingMessage msg = i.next();
builder.append(msg.asJson().findValue("instance").findValue("pointer").toString());
builder.append(": ");
builder.append(msg.getMessage());
String detail = msg.asJson().findValue("instance").findValue("pointer").toString() + ": " + msg.getMessage();
detail = detail.replace("\"", "");
validationErrorDetails.add(detail);
builder.append("\n");
}
return validationErrorDetails;
}
return null;
}
}
Address.java:
package de.s2.json.test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.reinert.jjschema.Attributes;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class Address
{
#JsonProperty("country")
private String country = "";
#JsonProperty("geoPosition")
#Attributes(required=false, description="longitude and latitude (it is initialized with 0,0)")
private GeoJsonPoint geoPosition = new GeoJsonPoint(0, 0);
}
Thank you very much for your support!
Update 1:
Answer to Hiran Chaudhuri
If the validation fails, then the schema looks like this:
{
"type": "object",
"properties": {
"country": {
"type": "string"
},
"geoPosition": {
"type": "object",
"properties": {
"coordinates": {
"type": "object"
},
"TYPE": {
"type": "string"
},
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"description": "longitude and latitude (it is initialized with 0,0 which is inside the ocean)"
}
},
"$schema": "http://json-schema.org/draft-04/schema#"
}
If it is successful, then it looks like this:
{
"type": "object",
"properties": {
"country": {
"type": "string"
},
"geoPosition": {
"type": "object",
"properties": {
"coordinates": {
"type": "array",
"items": {
"type": "number"
}
},
"TYPE": {
"type": "string"
},
"x": {
"type": "number"
},
"y": {
"type": "number"
}
},
"description": "longitude and latitude (it is initialized with 0,0 which is inside the ocean)"
}
},
"$schema": "http://json-schema.org/draft-04/schema#"
}
As already in the error message indicated, one time the coordinates are an 'object' the other time an 'array'
Update 2
As this issue is blocking me to continue with my project, I have added an "ugly hack" to check in the schema if it contains a GeoJsonPoint. If it is included, it will patch it with the correct values.
package de.s2.json.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
public class Toolbox {
public static <T> ArrayList<String> validateJson(T inputObj) throws ProcessingException {
com.github.reinert.jjschema.v1.JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
schemaFactory.setAutoPutDollarSchema(true);
JsonNode fstabSchema = schemaFactory.createSchema(inputObj.getClass());
ArrayList<String> validationErrorDetails = new ArrayList<String>();
// *****************************************************************************************
// /!\ this is an ugly hack /!\
// Randomly the function createSchema() returns an 'array' or 'object' type for coordinates
// of the geoJsonPoint. The correct value should be array. The following code checks if a
// geoJsonPoint is inside the schema and overwrites it with the correct value.
// TODO: fix this issue correctly
// *****************************************************************************************
try {
String jsonCoordinate = "{\"type\": \"array\",\"items\": {\"type\": \"number\"}}";
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNodeCoordinate = mapper.readTree(jsonCoordinate);
JsonNode coordJsonNode = fstabSchema.findValue("geoPosition").get("properties");
ObjectNode coordObjNode = (ObjectNode) coordJsonNode;
coordObjNode.set("coordinates", jsonNodeCoordinate);
} catch (JsonProcessingException e) {
validationErrorDetails.add("Could not patch geoPosition");
return validationErrorDetails;
} catch (IOException e) {
validationErrorDetails.add("Could not patch geoPosition");
return validationErrorDetails;
} catch (NullPointerException e) {
// this means that geoPosition could not be found
// we do nothing ...
}
// ******************
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(fstabSchema);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode baseReceiptJson = objectMapper.convertValue(inputObj, JsonNode.class);
ProcessingReport report;
report = schema.validate(baseReceiptJson);
if (!report.isSuccess()) {
StringBuilder builder = new StringBuilder();
builder.append("Not all required fields are filled with data");
builder.append(System.getProperty("line.separator"));
for (Iterator<ProcessingMessage> i = report.iterator(); i.hasNext();) {
ProcessingMessage msg = i.next();
builder.append(msg.asJson().findValue("instance").findValue("pointer").toString());
builder.append(": ");
builder.append(msg.getMessage());
String detail = msg.asJson().findValue("instance").findValue("pointer").toString() + ": " + msg.getMessage();
detail = detail.replace("\"", "");
validationErrorDetails.add(detail);
builder.append("\n");
}
return validationErrorDetails;
}
return null;
}
}

By serializing the JSON Schema you generated we are able now to compare whether they are the same or how much they actually differ. Take into account that the method to generate a schema requires some input parameter.
I was never a friend of generating a schema based on actual data.
Why do you not strictly define the schema you want and let your code measure whether you really meet that structure? This way you can use the schema as a contract between application components since it is documented and not generated on the fly.
This is still no answer to the question why this happens randomly. But it is maybe a better practice to follow for you and others reading along.

Related

Remove JSON node from json object (a json file) in Java

I'm trying to remove few nodes from JSON file. But I'm getting class cast exception when I try to remove node.
java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.TextNode cannot be cast to class com.fasterxml.jackson.databind.node.ObjectNode (com.fasterxml.jackson.databind.node.TextNode and com.fasterxml.jackson.databind.node.ObjectNode are in unnamed module of loader 'app')
I tried other stackoverflow useful links, but not working for me. Here is the code that I have written.
public static void removeNodes(String filePath, String... nodeNames) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNodes = objectMapper.readTree(new File(filePath));
for (JsonNode node : jsonNodes) {
((ObjectNode) node).remove(Arrays.asList(nodeNames));
}
objectMapper
.writerWithDefaultPrettyPrinter()
.writeValue(new File(filePath.split("\\.")[0] + "_modified.json"), jsonNodes);
}
This is the json file I'm reading.
{
"boolean_key": "--- true\n",
"empty_string_translation": "",
"key_with_description": "Check it out! This key has a description! (At least in some formats)",
"key_with_line-break": "This translations contains\na line-break.",
"nested": {
"deeply": {
"key": "Wow, this key is nested even deeper."
},
"key": "This key is nested inside a namespace."
},
"null_translation": null,
"pluralized_key": {
"one": "Only one pluralization found.",
"other": "Wow, you have %s pluralizations!",
"zero": "You have no pluralization."
},
"Dog_key": {
"nest": "Only one pluralization found.",
"sample_collection": [
"first item",
"second item",
"third item"
],
"Pest": "Only two pluralization found."
},
"simple_key": "Just a simple key with a simple message.",
"unverified_key": "This translation is not yet verified and waits for it. (In some formats we also export this status)"
}
Caller:
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
// import static org.junit.jupiter.api.Assertions.*;
#RunWith(SpringRunner.class)
#SpringBootTest
class JSONUtilTest {
#Test
void removeNodes() throws IOException {
JSONUtil.removeNodes("D:\\data\\JSON_A.json", "pluralized_key", "Dog_key", "simple_key");
}
}
Can you help me to figure out what is causing the problem?
As mentioned in the comment, you need to first parse the JSON then do operations on it.
Read file.
Parse and convert it to JSONObject using JSONParser's parse() method.
Remove the nodes you want.
Using ObjectMapper's writerWithDefaultPrettyPrinter() method you get proper indentation and write that to output file.
Here is the code:
public static void removeNodes(String filePath, String... nodeNames) {
try (FileReader fileReader = new FileReader(filePath)) {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(fileReader);
Stream<String> nodeStream = Stream.of(nodeNames);
nodeStream.forEach(jsonObject::remove);
ObjectMapper objectMapper = new ObjectMapper();
String jsonObjectPrettified = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
FileWriter fileWriter = new FileWriter(filePath.split("\\.")[0] + "_modified.json");
fileWriter.write(jsonObjectPrettified);
fileWriter.close();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}

How to get the last entry of a field in JSON

I use an API from api.nasa.gov that I query using the following Java class to read magnitude information on the latest solar flare of the day.
The response is used to fill in a field on a weather station that informs about current solar hazards.
The problem is that this code only returns the "classType" (magnitude) of first "flrID" (solar flare) event of the day. On July 3, for example, there was more than one event, and the most relevant event is the latest.
I am trying to find out how to get the "classType" of the last "flrID" in the JSON string, given that the flrID text contains an unknown arbitrary time of day.
Secondly, I am not sure how to get event driven updates from NASA, other than from pushed email messages. I am allowed an API request every 3.6 seconds, so that will work, but I am inquiring if there is a less expensive method to get near real time updates from NASA.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONException;
import org.json.JSONObject;
public class NasaFlrApiReader {
public static final String API_KEY = "DEMO_KEY";
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
try (InputStream is = new URL(url).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String jsonText = readAll(rd);
int i = jsonText.indexOf("{");
jsonText = jsonText.substring(i);
JSONObject json = new JSONObject(jsonText);
return json;
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://api.nasa.gov/DONKI/FLR?startDate=2021-07-27-03&endDate=2021-07-03&api_key=" + API_KEY);
System.out.println("Class Type: " + json.get("classType"));
}
}
This is the JSON text that is returned from the above code:
[
{
"flrID": "2021-07-03T02:14:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T02:14Z",
"peakTime": "2021-07-03T02:31Z",
"endTime": "2021-07-03T02:39Z",
"classType": "C5.7",
"sourceLocation": "N23W75",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T03:48:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17197/-1"
},
{
"flrID": "2021-07-03T07:04:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T07:04Z",
"peakTime": "2021-07-03T07:17Z",
"endTime": "2021-07-03T07:22Z",
"classType": "M2.7",
"sourceLocation": "N23W78",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T08:00:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17189/-1"
},
{
"flrID": "2021-07-03T14:18:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T14:18Z",
"peakTime": "2021-07-03T14:29Z",
"endTime": "2021-07-03T14:34Z",
"classType": "X1.5",
"sourceLocation": "N23W80",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T14:48:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17201/-1"
},
{
"flrID": "2021-07-03T16:59:00-FLR-001",
"instruments": [
{
"displayName": "GOES-P: EXIS 1.0-8.0"
}
],
"beginTime": "2021-07-03T16:59Z",
"peakTime": "2021-07-03T17:03Z",
"endTime": "2021-07-03T17:14Z",
"classType": "M1.0",
"sourceLocation": "N23W82",
"activeRegionNum": 12838,
"linkedEvents": [
{
"activityID": "2021-07-03T17:36:00-CME-001"
}
],
"link": "https://kauai.ccmc.gsfc.nasa.gov/DONKI/view/FLR/17208/-1"
}
]
The JSON contains an array so during deseralisation, you need to use JSONArray and then get the last element of the array using length-1,
JSONArray jsonArr = new JSONArray(jsonText);
JSONObject lastObject = (JSONObject) jsonArr.get(jsonArr.length()-1);
If length-1 does not give you the last record then you need to sort the JSONArray based on the required field and then take the first or last based on sorting. You can check the below thread for sorting the JSONArray
How can I sort a JSONArray in JAVA

How to dynamically create items in DynamoDB reading objects from a json file using java

my requirement is to read data from json file and create items in dynamoDB with the objects that are present in dynamoDB, for example consider this following file
{
"ISA": {
"isa01_name": "00",
"isa02": " ",
"isa03": "00",
"isa04": " ",
"isa05": "ZZ",
"isa06": "CLEARCUT ",
"isa07": "ZZ",
"isa08": "CMSENCOUNTERCTR",
"isa09": "120109",
"isa10": "1530",
"isa11": "U",
"isa12": "00501",
"isa13": "012412627",
"isa14": "0",
"isa15": "T",
"isa16": ":"
},
"GS": {
"gs02": "352091331",
"gs04": "20170109",
"gs06": "146823",
"gs03": "00580",
"gs05": "1530",
"gs01": "HC",
"gs08": "005010X222A1",
"gs07": "X"
},
"ST": {
"ST03_1705_Implementation Convention Reference": "005010X222A1",
"ST01_143_Transaction Set Identifier Code": "837",
"ST02_329_Transaction Set Control Number": "50138"
}
}
when i read this file it have to create ISA , GS , ST items in dynamodb database. Again if read another file having different objects , then items have to be created for them as well.
following is the code that i am having right now.
`import java.io.File;
import java.util.Iterator;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class LoadData {
public static void main(String ards[]) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("NewTable");
JsonParser parser = new JsonFactory().createParser(new File("C:\\Users\\Nikhil yadav\\Downloads\\healthclaims\\healthclaims\\src\\main\\resources\\output\\ValidJson.json"));
JsonNode rootNode = new ObjectMapper().readTree(parser);
Iterator<JsonNode> iter = rootNode.iterator();
ObjectNode currentNode;
while (iter.hasNext()) {
currentNode = (ObjectNode) iter.next();
try {
table.putItem(new Item().withPrimaryKey("ISA", currentNode.path("ISA").toString().replaceAll("[^:a-zA-Z0-9_-|]", " "))
.withString("GS",currentNode.path("GS").toString().replaceAll("[^:a-zA-Z0-9_-|]", " "))
.withString("ST", currentNode.path("ST").toString().replaceAll("[^:a-zA-Z0-9_-|]", " "))
);
System.out.println("PutItem succeeded: ");
}
catch (Exception e) {
System.err.println("Unable to add : ");
System.err.println(e.getMessage());
break;
}
}
parser.close();
}
}
`
it will only accept file having ISA , GS and ST objects, but i want program which accepts all types objects of json files.
i hope my question is clear. i am new in posting questions please ignore if it is not clear.

Unable to build this Complex JSON Structure

I have a inter-related Maps representing the below data .
{
"Soft Drinks": {
"Tin": [
{
"Lean Apple": [
{
"name": "1 Litre"
},
{
"name": "2 Litre"
}
]
},
{
"Clear": [
{
"name": "7 Litre"
},
{
"name": "10 Litre"
}
]
}
],
"Bottled": [
]
}
}
This is my code representing above json data in form of Java code
package test;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class Post {
public static void main(String args[]) throws JSONException
{
LinkedList<String> forTinItemsList = new LinkedList<String>();
LinkedList<String> forBottleItemsList = new LinkedList<String>();
JSONObject jsonobj = new JSONObject();
Map<String,LinkedList<String>> categoryitemsMap = new LinkedHashMap<String,LinkedList<String>>();
forTinItemsList.add("Lean Apple");
forTinItemsList.add("Clear");
forBottleItemsList.add("Lemon");
forBottleItemsList.add("Clear");
categoryitemsMap.put("Tin", forTinItemsList);
categoryitemsMap.put("Bottled", forBottleItemsList);
// completion of Categories.
Map<String,LinkedList<String>> subcategory = new LinkedHashMap<String,LinkedList<String>>();
LinkedList<String> forLemonItems = new LinkedList<String>();
forLemonItems.add("1 Litre");
forLemonItems.add("2 Litre");
subcategory.put("Lemon", forLemonItems);
LinkedList<String> forClearItems = new LinkedList<String>();
forClearItems.add("7 Litre");
forClearItems.add("10 Litre");
subcategory.put("Clear", forClearItems);
for (Map.Entry<String, LinkedList<String>> entry : categoryitemsMap.entrySet())
{
String key = entry.getKey();
LinkedList<String> list = entry.getValue();
for(String value : list)
{
System.out.println(key+"\t"+value);
}
//jsonobj.put(entry, arg1);
}
}
}
Could anybody please tell me how can i build the above JSON Structure ??
I was trying with different things m, but i was unsuccessful , the problem i was facing is that i am getting the Bottled Array is also filling up with the same Tin Array items .
To give you an idea of the unholy abomination things would become if you did it this way: here's the data structure that you would have to create to capture the JSON you propose.
Map<String, Map<String, List<Map<String, List<Map<String, String>>>>>> items;
So, feel free to implement this, but if it was me, I would build a data model and then map it with Jackson.
there are libraries that do this for you GSON and Jackson

Creating array list of java objects from JSON URL with Gson

I am able to parse the following data into a java object:
{
"name": "testname",
"address": "1337 455 ftw",
"type": "sometype",
"notes": "cheers mate"
}
using this code:
public class Test
{
public static void main (String[] args) throws Exception
{
URL objectGet = new URL("http://10.0.0.4/file.json");
URLConnection yc = objectGet.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
Gson gson = new Gson();
try {
DataO data = new Gson().fromJson(in, DataO.class);
System.out.println(data.getName());
}catch (Exception e) {
e.printStackTrace();
}
}
}
But now I want to store a list of these objects out of the following JSON String:
[
{
"name": "testname",
"address": "1337 455 ftw",
"type": "sometype",
"notes": "cheers mate"
},
{
"name": "SumYumStuff",
"address": "no need",
"type": "clunkdroid",
"notes": "Very inefficient but high specs so no problem."
}
]
Could someone help me modify my code to do this?
You could specify the type to deserialize into as an array or as a collection.
As Array:
import java.io.FileReader;
import com.google.gson.Gson;
public class GsonFoo
{
public static void main(String[] args) throws Exception
{
Data0[] data = new Gson().fromJson(new FileReader("input.json"), Data0[].class);
System.out.println(new Gson().toJson(data));
}
}
class Data0
{
String name;
String address;
String type;
String notes;
}
As List:
import java.io.FileReader;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonFoo
{
public static void main(String[] args) throws Exception
{
List<Data0> data = new Gson().fromJson(new FileReader("input.json"), new TypeToken<List<Data0>>(){}.getType());
System.out.println(new Gson().toJson(data));
}
}
A quick look in the Gson User Guide indicates that this might not be possible since the deserializer doesn't know the type of the elements since you could have elements of different types in the array.
Collections Limitations
Can serialize collection of arbitrary objects but can not deserialize
from it Because there is no way for the user to indicate the type of
the resulting object While deserializing, Collection must be of a
specific generic type

Categories

Resources