Skip to content

Tag: gson

Gson to append json object within an array in a json file in java

I am trying to store a game result in a JSON file. The results are stored when the game is over, and are supposed to be displayed afterwards in a table. I have this function that creates the game score and adds it to json file using GSON That method creates a JSON file that looks like this: The problem

How to set the classpath correctly in java

I need to compile and run simple code using the gson library, but I can’t use Maven, Gradle or the IDE. The directory contains Main.java and gson-2.9.0.jar javac -cp gson-2.9.0.jar Main.java works correctly and creates Main.class But when I run java -cp ./*: Main, I get I also tried the following comman…

Streaming large JSON from input stream efficiently in Java

In order to save memory and avoid an OOM error, I want to stream a large JSON from an input stream and extract the desired things from it. More exactly, I want to extract and save some strings from that JSON: files.content.fileContent.subList.text = “some text in file” files.content.fileContent.su…

Java Date to MongoDB date with Gson

Introduction I am currently working on a project that regularly saves several java objects to a MongoDB database. This object contains a Date. Problem The conversion of java Date to Json Mongo give this: But this format does not conform with MongoDB Date. MongoDB is considered as a string instead of a Date. A…

GSON flat down map to other fields

So I have an Android app which uses Retrofit for API. I have a class like which looks like: When GSON creates a JSON it looks like: Would it be possible to change JSON serialization to: Thanks. Answer Here is how Gson could be used to implement the flattening: I’ve put some comments explaining “wh…