Related
I want to serialize a Map with Jackson.
The Date should be serialized as a timestamp, like all my other dates.
The following code renders the keys in the form "Tue Mar 11 00:00:00 CET 1952" (which is Date.toString()) instead of the timestamp.
Map<Date, String> myMap = new HashMap<Date, String>();
...
ObjectMapper.writeValue(myMap)
I assume this is because of type erasure and jackson doesn't know at runtime that the key is a Date. But I didn't find a way to pass a TypeReference to any writeValue method.
Is there a simple way to achieve my desired behaviour or are all keys always rendered as Strings by jackson?
Thanks for any hint.
The default map key serializer is StdKeySerializer, and it simply does this.
String keyStr = (value.getClass() == String.class) ? ((String) value) : value.toString();
jgen.writeFieldName(keyStr);
You could make use of the SimpleModule feature, and specify a custom key serializer, using the addKeySerializer method.
And here's how that could be done.
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.map.type.MapType;
import org.codehaus.jackson.map.type.TypeFactory;
public class CustomKeySerializerDemo
{
public static void main(String[] args) throws Exception
{
Map<Date, String> myMap = new HashMap<Date, String>();
myMap.put(new Date(), "now");
Thread.sleep(100);
myMap.put(new Date(), "later");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(myMap));
// {"Mon Jul 04 11:38:36 MST 2011":"now","Mon Jul 04 11:38:36 MST 2011":"later"}
SimpleModule module =
new SimpleModule("MyMapKeySerializerModule",
new Version(1, 0, 0, null));
module.addKeySerializer(Date.class, new DateAsTimestampSerializer());
MapType myMapType = TypeFactory.defaultInstance().constructMapType(HashMap.class, Date.class, String.class);
ObjectWriter writer = new ObjectMapper().withModule(module).typedWriter(myMapType);
System.out.println(writer.writeValueAsString(myMap));
// {"1309806289240":"later","1309806289140":"now"}
}
}
class DateAsTimestampSerializer extends JsonSerializer<Date>
{
#Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException
{
jgen.writeFieldName(String.valueOf(value.getTime()));
}
}
Update for the latest Jackson (2.0.4):
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
public class CustomKeySerializerDemo
{
public static void main(String[] args) throws Exception
{
Map<Date, String> myMap = new HashMap<Date, String>();
myMap.put(new Date(), "now");
Thread.sleep(100);
myMap.put(new Date(), "later");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(myMap));
// {"2012-07-13T21:14:09.499+0000":"now","2012-07-13T21:14:09.599+0000":"later"}
SimpleModule module = new SimpleModule();
module.addKeySerializer(Date.class, new DateAsTimestampSerializer());
MapType myMapType = TypeFactory.defaultInstance().constructMapType(HashMap.class, Date.class, String.class);
ObjectWriter writer = new ObjectMapper().registerModule(module).writerWithType(myMapType);
System.out.println(writer.writeValueAsString(myMap));
// {"1342214049499":"now","1342214049599":"later"}
}
}
class DateAsTimestampSerializer extends JsonSerializer<Date>
{
#Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException
{
jgen.writeFieldName(String.valueOf(value.getTime()));
}
}
As usual, Bruce's answer is right on the spot.
One additional thought is that since there is a global setting for serializing Date values as timestamps:
SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS
Maybe that should apply here as well. And/or at least use standard ISO-8601 format for text. The main practical issue there is that of backwards compatibility; however, I doubt that current use of plain toString() is very useful as it is neither efficient nor convenient (to read back the value).
So if you want, you might want to file a feature request; this sounds like sub-optimal handling of Map keys by Jackson.
Since Jackson 2.0 (maybe 1.9, too), WRITE_DATE_KEYS_AS_TIMESTAMPS can be used to change this particular behavior.
Usage example for ObjectMapper:
ObjectMapper m = new ObjectMapper().configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, true);
and for ObjectWriter:
ObjectWriter w = mapper.with(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);
I'm using the Jackson set of classes to read in a CSV file, and convert it to xml, but need some advice on how to add a nested value.
The code I'm using is:
package reader;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class Mustang
{
public static void main(String[] args) throws Exception
{
// Define the input file
File input = new File("c:\\temp\\all_idocs.csv");
// Define the output file
File output = new File("c:\\temp\\all_idocs.xml");
System.out.println("INFO: Commencing Conversion");
List<Map<?, ?>> data = readObjectsFromCsv(input); // readObjectsFromCSV(input
// file name)
System.out.println(readObjectsFromCsv(input));
writeAsXml(data, output); // writeAsXml function, to output location
System.out.println("INFO: Conversion Complete");
}
public static List<Map<?, ?>> readObjectsFromCsv(File file)
throws IOException
{
CsvSchema schema = CsvSchema.builder()
.addColumn("A0001")
.addColumn("A0186")
.addColumn("A0187")
.addColumn("A0352")
.addColumn("A0539")
.addColumn("A0963")
.addColumn("A1046")
.addColumn("A0792")
.addColumn("A0218")
.addColumn("A0584")
.addColumn("A0016")
.addColumn("A0017")
.addColumn("A0478")
.addColumn("A0051")
.addColumn("A0052")
.addColumn("A0053")
.addColumn("A0059")
.addColumn("A0440")
.addColumn("A0054")
.addColumn("A0055")
.addColumn("A0056")
.addColumn("A0057")
.addColumn("A0058")
.addColumn("A1128")
.addColumn("A0003")
.addColumn("A0069")
.addColumn("A0070")
.addColumn("A0074")
.addColumn("A0073")
.addColumn("A0071")
.addColumn("A0110")
.addColumn("A0109")
.addColumn("A0108")
.build();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<?, ?>> mappingIterator = csvMapper
.reader(Map.class).with(schema).readValues(file); // Change the "with()" to pull in the schema
return mappingIterator.readAll();
}
public static void writeAsXml(List<Map<?, ?>> data, File file)
throws IOException
{
XmlMapper mapper = new XmlMapper();
mapper.writeValue(file, data);
}
}
If I run this against a CSV file, I get output similar to this:
<item>
<A0001>J1000097</A0001>
<A0186>5028197000004</A0186>
<A0187>1</A0187>
<A0352></A0352>
<A0539>00</A0539>
<A0963>20050209</A0963>
</item>
I want to see if it's possible to indent/nest some of these attributes, to produce something like this:
<item>
<A0001>J1000097</A0001>
<A0186>5028197000004</A0186>
<A0187>
<A0352>12</A0352>
<A0539>00</A0539>
</A0187>
<A0963>20050209</A0963>
</item>
I'm assuming I must have to do something within the builder section of the code, but as I'm new to using it, I can't fathom out how.
In this case you may want to process Map after reading it from CSV, but before writing it as XML. You can then add a wrapper around values you want to group. That is, something like:
Map<?,?> value = ... ; // individual row
Map<String,Object> wrapped = new LinkedHashMap<>();
wrapped.put("A0352", value.remove("A0352"));
wrapped.put("A0539", value.remove("A0539"));
value.put("A0187", wrapped);
another possibility would be to use value conversion between Map, and POJO type that uses #JsonUnwrapped to handle grouping.
Conversion itself may be done using:
MyPOJO value = mapper.convertValue(map, MyPOJO.class); // and/or reverse
but this approach may become more complicated.
I get some JSON code like this:
{"1":{"id":"1","Vorname":"x","Nachname":"y","MaleFemale":0,"interests":[]},
"2":{"id":"2","Vorname":"x","Nachname":"y","MaleFemale":1,"interests":[]},
...
from my PHP script. Could you tell me how to decode this format in Java?
I only get examples where you have to have to have a format like this:
{"contacts": [{"user.id":"1","Vorname":"x","Nachname":"y","MaleFemale":1},
{"user.id":"2","Vorname":"x1","Nachname":"y2","MaleFemale":0}]}
So the difference is that in the first given code there is no "main node". In the second given code there is one ("contacts"). Do I need this node? I try so much but i do not get how to work this out.
Thank you very much.
I thinks you should use jackson mapper. here is a link:
How to convert Java object to / from JSON (Jackson)
You can do this easily with Jackson java library. Here is an example code snippet.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
// Reading the string to a JSON object
JsonNode jsonObject = mapper.readTree("{\"contacts\": [{\"user.id\":\"1\",\"Vorname\":\"x\",\"Nachname\":\"y\",\"MaleFemale\":1},\n" +
" {\"user.id\":\"2\",\"Vorname\":\"x1\",\"Nachname\":\"y2\",\"MaleFemale\":0}]}");
//Some basic querying
JsonNode contacts = jsonObject.get("contacts");
if (contacts.isArray()){
ArrayNode contactsArray = (ArrayNode) contacts;
for (JsonNode contact : contactsArray) {
System.out.println(contact.get("user.id"));
}
}
}
}
You can download the Jackson library from here.
I have the following JSON snippets:
{ "randomlygeneratedKeyname0" : "some-value",
"randomlygeneratedKeyname1": {
"randomlygeneratedKeyname2" : {
"randomlygeneratedKeyname3": "some-value",
"randomlygeneratedKeyname4": "some-value"
},
"randomlygeneratedKeyname5": {
"randomlygeneratedKeyname6": "some-value",
"randomlygeneratedKeyname7": "some-value"
}
}
}
Notes that I don't know the name of randomlygeneratedKeyname and their naming convention is inconsistent so I could not create my corresponding Java field/variable names.
How do I (de)serialize it in GSON?
Thanks in advance for your help.
I'm pleased to report that with GSON 2.0 supports default maps and lists without effort. Deserialize like this:
Object o = new Gson().fromJson(json, Object.class);
The result will be a Map with String keys and either String or Map values.
Serialize that map back to JSON like this:
String json = new Gson().toJson(o);
We hope to release GSON 2.0 in October 2012. You can get it early from GSON SVN.
Code dump solution:
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
public class Foo
{
public static void main(String[] args) throws Exception
{
Type mapStringObjectType = new TypeToken<Map<String, Object>>() {}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(mapStringObjectType, new RandomMapKeysAdapter());
Gson gson = gsonBuilder.create();
Map<String, Object> map = gson.fromJson(new FileReader("input.json"), mapStringObjectType);
System.out.println(map);
}
}
class RandomMapKeysAdapter implements JsonDeserializer<Map<String, Object>>
{
#Override
public Map<String, Object> deserialize(JsonElement json, Type unused, JsonDeserializationContext context)
throws JsonParseException
{
// if not handling primitives, nulls and arrays, then just
if (!json.isJsonObject()) throw new JsonParseException("some meaningful message");
Map<String, Object> result = new HashMap<String, Object> ();
JsonObject jsonObject = json.getAsJsonObject();
for (Entry<String, JsonElement> entry : jsonObject.entrySet())
{
String key = entry.getKey();
JsonElement element = entry.getValue();
if (element.isJsonPrimitive())
{
result.put(key, element.getAsString());
}
else if (element.isJsonObject())
{
result.put(key, context.deserialize(element, unused));
}
// if not handling nulls and arrays
else
{
throw new JsonParseException("some meaningful message");
}
}
return result;
}
}
What is the best way to convert a JSON code as this:
{
"data" :
{
"field1" : "value1",
"field2" : "value2"
}
}
in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).
Any ideas? Should I use Json-lib for that? Or better if I write my own parser?
I hope you were joking about writing your own parser. :-)
For such a simple mapping, most tools from http://json.org (section java) would work.
For one of them (Jackson https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator), you'd do:
Map<String,Object> result =
new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);
(where JSON_SOURCE is a File, input stream, reader, or json content String)
Using the GSON library:
import com.google.gson.Gson;
import com.google.common.reflect.TypeToken;
import java.lang.reclect.Type;
Use the following code:
Type mapType = new TypeToken<Map<String, Map>>(){}.getType();
Map<String, String[]> son = new Gson().fromJson(easyString, mapType);
I like google gson library.
When you don't know structure of json. You can use
JsonElement root = new JsonParser().parse(jsonString);
and then you can work with json. e.g. how to get "value1" from your gson:
String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();
Use JSON lib E.g. http://www.json.org/java/
// Assume you have a Map<String, String> in JSONObject jdata
#SuppressWarnings("unchecked")
Iterator<String> nameItr = jdata.keys();
Map<String, String> outMap = new HashMap<String, String>();
while(nameItr.hasNext()) {
String name = nameItr.next();
outMap.put(name, jdata.getString(name));
}
My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:
{
"shopping_list":{
"996386":{
"id":996386,
"label":"My 1st shopping list",
"current":true,
"nb_reference":6
},
"888540":{
"id":888540,
"label":"My 2nd shopping list",
"current":false,
"nb_reference":2
}
}
}
To parse this JSON file with GSON library, it's easy :
if your project is mavenized
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Then use this snippet :
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
//Read the JSON file
JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));
//Get the content of the first map
JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();
//Iterate over this map
Gson gson = new Gson();
for (Entry<String, JsonElement> entry : object.entrySet()) {
ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
System.out.println(shoppingList.getLabel());
}
The corresponding POJO should be something like that :
public class ShoppingList {
int id;
String label;
boolean current;
int nb_reference;
//Setters & Getters !!!!!
}
Hope it helps !
With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:
Map map = gson.fromJson(json, Map.class);
Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.
This way its works like a Map...
JSONObject fieldsJson = new JSONObject(json);
String value = fieldsJson.getString(key);
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.1</version>
</dependency>
I do it this way. It's Simple.
import java.util.Map;
import org.json.JSONObject;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
JSONObject jsonObj = new JSONObject("{ \"f1\":\"v1\"}");
#SuppressWarnings("unchecked")
Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
System.out.println(map);
}
}
java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Gson gson = new Gson();
Map<String, Object> categoryicons = gson.fromJson(json, mapType );
If you're using org.json, JSONObject has a method toMap().
You can easily do:
Map<String, Object> myMap = myJsonObject.toMap();
The JsonTools library is very complete. It can be found at Github.
Try this code:
public static Map<String, Object> convertJsonIntoMap(String jsonFile) {
Map<String, Object> map = new HashMap<>();
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
mapper.readValue(jsonFile, new TypeReference<Map<String, Object>>() {
});
map = mapper.readValue(jsonFile, new TypeReference<Map<String, String>>() {
});
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
One more alternative is json-simple which can be found in Maven Central:
(JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.
The artifact is 24kbytes, doesn't have other runtime dependencies.
If you need pure Java without any dependencies, you can use build in Nashorn API from Java 8. It is deprecated in Java 11.
This is working for me:
...
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
...
public class JsonUtils {
public static Map parseJSON(String json) throws ScriptException {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("javascript");
String script = "Java.asJSONCompatible(" + json + ")";
Object result = engine.eval(script);
return (Map) result;
}
}
Sample usage
JSON:
{
"data":[
{"id":1,"username":"bruce"},
{"id":2,"username":"clark"},
{"id":3,"username":"diana"}
]
}
Code:
...
import jdk.nashorn.internal.runtime.JSONListAdapter;
...
public static List<String> getUsernamesFromJson(Map json) {
List<String> result = new LinkedList<>();
JSONListAdapter data = (JSONListAdapter) json.get("data");
for(Object obj : data) {
Map map = (Map) obj;
result.add((String) map.get("username"));
}
return result;
}
JSON to Map always gonna be a string/object data type. i have GSON lib from google.
Gson library working with string not for complex objects you need to do something else
Try this piece of code, it worked for me,
Map<String, Object> retMap = new Gson().fromJson(
myJsonString, new TypeToken<HashMap<String, Object>>() {}.getType()
);
import net.sf.json.JSONObject
JSONObject.fromObject(yourJsonString).toMap
Underscore-java library can convert json string to hash map. I am the maintainer of the project.
Code example:
import com.github.underscore.U;
import java.util.*;
public class Main {
#SuppressWarnings("unchecked")
public static void main(String[] args) {
String json = "{"
+ " \"data\" :"
+ " {"
+ " \"field1\" : \"value1\","
+ " \"field2\" : \"value2\""
+ " }"
+ "}";
Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
System.out.println(data);
// {field1=value1, field2=value2}
}
}
JSON to Map always gonna be a string/object data type. i haved GSON lib from google.
works very well and JDK 1.5 is the min requirement.