My code:
private List<Day> readDays(File file) {
List<Day> days = new ArrayList<>();
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
days.addAll((List<Day>) in.readObject());
} catch (IOException | ClassNotFoundException e) {
Logger.logError(LOG_TAG, e);
}
return days;
}
Unchecked cast problem in this code
days.addAll((List<Day>) in.readObject());
And this is a problem, in some cases the app crashes.
if your problem is cast object; you can define a convertor to convert your object to your class and handle exceptions.
if your stream return json string, You can use ObejctMapper and convert json string to your class as follow method by using jackson library:
//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();
//convert json string to object
Day day = objectMapper.readValue(jsonData, Day.class);
// use day class now
so converting object, depend on your file data format.
Related
I've been trying to serialize LocalDateTime to a string and deserialize the same String back into a LocalDateTime object.
Using the following code:
public void testSerialization(){
Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
serializer.setObjectMapper(objectMapper);
ByteArrayOutputStream out = new ByteArrayOutputStream();
String results = "";
Map<String, Object> resultMap;
// Object I want to serialize and deserialize
HashMap<String, Object> map = new HashMap<>();
LinkedHashMap<String, Object> lMap = new LinkedHashMap<>();
lMap.put("DATE", LocalDateTime.of(2022,4,12,1,54));
map.put("reader.start.after", lMap);
// serialize
try{
serializer.serialize(map, out);
results = new String(out.toByteArray(), "ISO-8859-1");
}catch (Exception e){
System.out.println(e);
}
}
This serializes the object to:
{"reader.start.after":{"DATE":"2022-04-12T01:54:00"}}
When I attempt to deserialize using:
Map<String, Object> resultMap;
try{
ByteArrayInputStream in = new ByteArrayInputStream(results.getBytes("ISO-8859-1"));
resultMap = serializer.deserialize(in);
}catch (Exception e){
System.out.println(e);
}
It almost deserializes everything correctly except it doesn't deserialize "2022-04-12T01:54:00" back into a LocalDateTime. Does anyone know how to have it deserialize back into LocalDateTime and not a String?
I got a JSON that I serialize to a MongoDB BasicDBObject and insert it into the DB:
String serialized = "";
try {
serialized = OBJECT_MAPPER.writeValueAsString(customEx.getOut().getBody());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
collection.update(upsertQuery, BasicDBObject.parse(serialized), true, false);
On reading the DBObject from the DB I want to convert it to a POJO using the ObjectMappers' readValue() with a given class:
public static <T> T fromDB(DBObject o, Class<T> clazz) {
try {
return OBJECT_MAPPER.readValue(o.toString(), clazz);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
The class I want to convert it to is generated from an xsd sheme and also contains timestamps/long values as following:
#XmlRootElement(name = "ItemType")
public class ItemType {
#XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar date;
[...]
However this worked fine for older Java MongoDB versions. Now the long values are serialized as BSON looking like this:
"date": {
"$numberLong": "1551172199214"
}
When I try to deserialize this using jacksons ObjectMapper I get
Cannot deserialize instance of javax.xml.datatype.XMLGregorianCalendar out of START_OBJECT token
The reason for this is clear to me because the long value is in an own BSON - Style object.
So far I already tried using BsonDocument like this:
public static <T> T fromDB(DBObject o, Class<T> clazz) {
try {
BsonDocument parse = BsonDocument.parse(o.toString());
return OBJECT_MAPPER.readValue(parse.toJson(), clazz);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
But that still does not convert the BSON parts to JSON.
Is there any way to deserialize the BSON to a given Class using Jacksons ObjectMapper?
Or just convert it to a DBObject without using BSON parts?
If calendar field is wrapped we need to unwrap it. We can extend already implemented CoreXMLDeserializers.GregorianCalendarDeserializer:
class XmlGregorianCalendarDeserializer extends CoreXMLDeserializers.GregorianCalendarDeserializer {
#Override
public XMLGregorianCalendar deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
jp.nextToken(); // Skip FIELD_NAME
jp.nextToken(); // Skip VALUE_STRING
XMLGregorianCalendar calendar = super.deserialize(jp, ctxt);
jp.nextToken(); // Skip END_OBJECT
return calendar;
}
}
If XMLGregorianCalendar is always wrapped we can register this deserialiser using SimpleModule. See below example:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ext.CoreXMLDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import javax.xml.datatype.XMLGregorianCalendar;
public class Test {
public static void main(String[] args) throws Exception {
SimpleModule wrappedCalendarModule = new SimpleModule();
wrappedCalendarModule.addDeserializer(XMLGregorianCalendar.class, new XmlGregorianCalendarDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(wrappedCalendarModule);
String json = "{\n"
+ " \"date\":{\n"
+ " \"$numberLong\":\"1551172199214\"\n"
+ " },\n"
+ " \"name\":\"Rick\"\n"
+ "}";
System.out.println(mapper.readValue(json, Wrapper.class));
}
}
class Wrapper {
private XMLGregorianCalendar date;
private String name;
// getters, setters, toString
}
Above code prints:
Wrapper{date=2019-02-26T09:09:59.214Z, name='Rick'}
It looks like you need to configure the ObjectMapper to deserialize the XMLGregorianCalendar differently:
public class XMLGregorianCalendarDeserializer extends JsonDeserializer<XMLGregorianCalendar> {
private ObjectMapper objectMapper = new ObjectMapper();
#Override
public XMLGregorianCalendar deserialize(
JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
Map<String, String> bsonStringAsMap =
objectMapper.readValue(
jsonParser.readValueAsTree().toString(), new TypeReference<Map<String, String>>() {});
String timestampString = bsonStringAsMap.get("$numberLong");
if (timestampString != null && !timestampString.isEmpty()) {
long timestamp = Long.parseLong(timestampString);
Date date = new Date(timestamp);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
}
return null;
}
}
Then you can annotate the field with this and deserialize as you have been:
#JsonDeserialize(using=XMLGregorianCalendarDeserializer.class)
#XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar date;
I am using Jackson and am able to get a JSONObject. I need to be able to convert this JSONObject to its json form. Meaning, the object that is represented by this JSONObject's son string.
Something like:
JsonObject object = ...;
object.toJsonString();
A simple Google search surprisingly didn't turn up many response and I am unable to see how to do it on my own.
Any ideas?
Try,
JSONObject object = ...;
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(object);
StringWriter out = new StringWriter();
object.writeJSONString(out);
String jsonText = out.toString();
System.out.print(jsonText);
If you need to bridge the 2 APIs you can create a custom StdSerializer.
More on custom serializers:
https://www.baeldung.com/jackson-custom-serialization
private static class JSONObjectSerializer extends StdSerializer<JSONObject> {
JSONObjectSerializer(){
this(null);
}
JSONObjectSerializer(Class<JSONObject> t) {
super(t);
}
#Override public void serialize(JSONObject value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
value.keys().forEachRemaining(key-> {
try {
gen.writeStringField(key,value.getString(key));
} catch (IOException ex){
throw new RuntimeException("Encountered an error serializing the JSONObject.",ex);
}
});
gen.writeEndObject();
}
private static SimpleModule toModule(){
return new SimpleModule().addSerializer(JSONObject.class, new JSONObjectSerializer());
}
}
//.......
ObjectWriter writer = new ObjectMapper()
.registerModule(JSONObjectSerializer.toModule())
.writerWithDefaultPrettyPrinter();
//.......
try {
s = w.writeValueAsString(v);// v being your JSONObject or parent
} catch (JsonProcessingException ex) {
throw new RuntimeException("Unable to write object to json.", ex);
}
I am trying to convert json from a text file into a java object.
I have tried both the jackson library, I put in the dependency and what not. My json file has both camel case and underscores, and that is causing an error when running my program. Here is the code that I used for when relating to the gson librar and it does not do anything, the output is the same with or without the code that I placed.
java.net.URL url = this.getClass().getResource("/test.json");
File jsonFile = new File(url.getFile());
System.out.println("Full path of file: " + jsonFile);
try
{
BufferedReader br = new BufferedReader(new FileReader("/test.json"));
// convert the json string back to object
DataObject obj = gson.fromJson(br, DataObject.class);
System.out.println(obj);
} catch (IOException e)
{
e.printStackTrace();
}
Now I also tried the jackson library. Here is the code i used
java.net.URL url = this.getClass().getResource("/test.json");
File jsonFile = new File(url.getFile());
System.out.println("Full path of file: " + jsonFile);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
InputStream is = Test_Project.class.getResourceAsStream("/test.json");
SampleDto testObj = mapper.readValue(is, SampleDto.class);
System.out.println(testObj.getCreatedByUrl());
I am not sure what to do,
This simple example works like a charm:
DTOs
public class SampleDTO
{
private String name;
private InnerDTO inner;
// getters/setters
}
public class InnerDTO
{
private int number;
private String str;
// getters/setters
}
Gson
BufferedReader br = new BufferedReader(new FileReader("/tmp/test.json"));
SampleDTO sample = new Gson().fromJson(br, SampleDTO.class);
Jackson
InputStream inJson = SampleDTO.class.getResourceAsStream("/test.json");
SampleDTO sample = new ObjectMapper().readValue(inJson, SampleDTO.class);
JSON (test.json)
{
"name" : "Mike",
"inner": {
"number" : 5,
"str" : "Simple!"
}
}
public static void main(String args[]){
ObjectMapper mapper = new ObjectMapper();
/**
* Read object from file
*/
Person person = mapper.readValue(new File("/home/document/person.json"), Person.class);
System.out.println(person);
}
A common way of getting both array of json in file or simply json would be
InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, Employee.class);
List<Employee> lstEmployees = mapper.readValue(inputStream, collectionType);
The file.json needs to be placed in the resources folder. If your file only has a json block without json array square brackets [] , you can skip the CollectionType
InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
Employee employee = mapper.readValue(inputStream, Employee.class);
Also refer here for original question from where I have drawn.
I am using java to call a url that returns a JSON object:
url = new URL("my URl");
urlInputStream = url.openConnection().getInputStream();
How can I convert the response into string form and parse it?
I would suggest you have to use a Reader to convert your InputStream in.
BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
new JSONObject(responseStrBuilder.toString());
I tried in.toString() but it returns:
getClass().getName() + '#' + Integer.toHexString(hashCode())
(like documentation says it derives to toString from Object)
All the current answers assume that it is okay to pull the entire JSON into memory where the advantage of an InputStream is that you can read the input little by little. If you would like to avoid reading the entire Json file at once then I would suggest using the Jackson library (which is my personal favorite but I'm sure others like Gson have similar functions).
With Jackson you can use a JsonParser to read one section at a time. Below is an example of code I wrote that wraps the reading of an Array of JsonObjects in an Iterator. If you just want to see an example of Jackson, look at the initJsonParser, initFirstElement, and initNextObject methods.
public class JsonObjectIterator implements Iterator<Map<String, Object>>, Closeable {
private static final Logger LOG = LoggerFactory.getLogger(JsonObjectIterator.class);
private final InputStream inputStream;
private JsonParser jsonParser;
private boolean isInitialized;
private Map<String, Object> nextObject;
public JsonObjectIterator(final InputStream inputStream) {
this.inputStream = inputStream;
this.isInitialized = false;
this.nextObject = null;
}
private void init() {
this.initJsonParser();
this.initFirstElement();
this.isInitialized = true;
}
private void initJsonParser() {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonFactory jsonFactory = objectMapper.getFactory();
try {
this.jsonParser = jsonFactory.createParser(inputStream);
} catch (final IOException e) {
LOG.error("There was a problem setting up the JsonParser: " + e.getMessage(), e);
throw new RuntimeException("There was a problem setting up the JsonParser: " + e.getMessage(), e);
}
}
private void initFirstElement() {
try {
// Check that the first element is the start of an array
final JsonToken arrayStartToken = this.jsonParser.nextToken();
if (arrayStartToken != JsonToken.START_ARRAY) {
throw new IllegalStateException("The first element of the Json structure was expected to be a start array token, but it was: " + arrayStartToken);
}
// Initialize the first object
this.initNextObject();
} catch (final Exception e) {
LOG.error("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
throw new RuntimeException("There was a problem initializing the first element of the Json Structure: " + e.getMessage(), e);
}
}
private void initNextObject() {
try {
final JsonToken nextToken = this.jsonParser.nextToken();
// Check for the end of the array which will mean we're done
if (nextToken == JsonToken.END_ARRAY) {
this.nextObject = null;
return;
}
// Make sure the next token is the start of an object
if (nextToken != JsonToken.START_OBJECT) {
throw new IllegalStateException("The next token of Json structure was expected to be a start object token, but it was: " + nextToken);
}
// Get the next product and make sure it's not null
this.nextObject = this.jsonParser.readValueAs(new TypeReference<Map<String, Object>>() { });
if (this.nextObject == null) {
throw new IllegalStateException("The next parsed object of the Json structure was null");
}
} catch (final Exception e) {
LOG.error("There was a problem initializing the next Object: " + e.getMessage(), e);
throw new RuntimeException("There was a problem initializing the next Object: " + e.getMessage(), e);
}
}
#Override
public boolean hasNext() {
if (!this.isInitialized) {
this.init();
}
return this.nextObject != null;
}
#Override
public Map<String, Object> next() {
// This method will return the current object and initialize the next object so hasNext will always have knowledge of the current state
// Makes sure we're initialized first
if (!this.isInitialized) {
this.init();
}
// Store the current next object for return
final Map<String, Object> currentNextObject = this.nextObject;
// Initialize the next object
this.initNextObject();
return currentNextObject;
}
#Override
public void close() throws IOException {
IOUtils.closeQuietly(this.jsonParser);
IOUtils.closeQuietly(this.inputStream);
}
}
If you don't care about memory usage, then it would certainly be easier to read the entire file and parse it as one big Json as mentioned in other answers.
For those that pointed out the fact that you can't use the toString method of InputStream like this see https://stackoverflow.com/a/5445161/1304830 :
My correct answer would be then :
import org.json.JSONObject;
public static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
...
JSONObject json = new JSONObject(convertStreamToString(url.openStream());
If you like to use Jackson Databind (which Spring uses by default for its HttpMessageConverters), then you may use the ObjectMapper.readTree(InputStream) API. For example,
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(myInputStream);
use jackson to convert json input stream to the map or object http://jackson.codehaus.org/
there are also some other usefull libraries for json, you can google: json java
Use a library.
GSON
Jackson
or one of many other JSON libraries that are out there.
Kotlin version with Gson
to read the response JSON:
val response = BufferedReader(
InputStreamReader(conn.inputStream, "UTF-8")
).use { it.readText() }
to parse response we can use Gson:
val model = Gson().fromJson(response, YourModelClass::class.java)
This example reads all objects from a stream of objects,
it is assumed that you need CustomObjects instead of a Map:
ObjectMapper mapper = new ObjectMapper();
JsonParser parser = mapper.getFactory().createParser( source );
if(parser.nextToken() != JsonToken.START_ARRAY) {
throw new IllegalStateException("Expected an array");
}
while(parser.nextToken() == JsonToken.START_OBJECT) {
// read everything from this START_OBJECT to the matching END_OBJECT
// and return it as a tree model ObjectNode
ObjectNode node = mapper.readTree(parser);
CustomObject custom = mapper.convertValue( node, CustomObject.class );
// do whatever you need to do with this object
System.out.println( "" + custom );
}
parser.close();
This answer was composed by using : Use Jackson To Stream Parse an Array of Json Objects and Convert JsonNode into Object
I suggest use javax.json.Json factory as less verbose possible solution:
JsonObject json = Json.createReader(yourInputStream).readObject();
Enjoy!
if you have JSON file you can set it on assets folder then call it using this code
InputStream in = mResources.getAssets().open("fragrances.json");
// where mResources object from Resources class
{
InputStream is = HTTPClient.get(url);
InputStreamReader reader = new InputStreamReader(is);
JSONTokener tokenizer = new JSONTokener(reader);
JSONObject jsonObject = new JSONObject(tokenizer);
}