How to serialize a map<Integer, Custom object> with Jackson streaming API? - java

Say that I've got a
class Person {
ArrayList<MyOtherObject> lstObjects;
...
}
and then
Map<Integer, Person> personMap
and want to serialize that map with Jackson Streaming API?
JsonGenerator g =...;
g.writeArrayFieldStart("PersonMap");
if (personMap != null) {
Iterator<Map.Entry<Integer, Person>> iter = personMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<Integer, Person> pairs = iter.next();
Integer key = (Integer) pairs.getKey();
Person person = (Person) pairs.getValue();
g.writeNumber(key.intValue());
person.saveToFileRaw(g); // Write the object
}
}
g.writeEndArray(); // PersonMap
and person.saveToFileRaw looks like
try {
g.writeStartObject();
g.writeObjectFieldStart("Inf");
if (lstInfo != null) {
for (PersonInfo info: lstInfo)
info.saveToFileRaw(g); // Write another object
}
g.writeEndObject();
String s = PersonType.token(type);
g.writeStringField("Tp", s);
g.writeStringField("Add", address);
So the question: how to write an array/map of custom objects? g.writeStartObject() in person.saveToFileRaw throws an exception saying that it expects a value.
Any ideas how to do this?

If you get an exception from JsonGenerator calls, you are trying to create invalid JSON structure; something that could not be parsed.
One problem I see in the code is that you first call "g.writeObjectFieldStart("Inf")", but then in loop try to call method which starts with "g.writeStartObject" -- essentially trying write start-object marker "{" twice.
You can also call "writeFieldName" separately (instead of writeObjectFieldStart()) which you probably need to do. Or maybe you need to do writeStartArray(() / writeEndArray() for PersonInfo entries; this depends on what exact output you want.

Related

Retrieving Values from having a particular property from a Map using Java 8 Stream

I have a class UserCourseEntity with a property userId
#AllArgsConstructor
#Getter
#ToString
public static class UserCourseEntity {
private String userId;
}
And I have a map with UserCourseEntity objects as values.
public final Map<String, UserCourseEntity> userCourses;
Method getUserCoursesByUserID receives userId property of the UserCourseEntity as a parameter.
I want to check if there are values in the userCourses map having userId that matches the giving id in the case-insensitive manner (i.e. using equalsIgnoreCase()).
If there are such values, I need to store them into a list, and throw an exception otherwise.
I'm wonder is it possible to reimplement this code using streams?
public List<UserCourseEntity> getUserCoursesByUserID(String userId) {
List<UserCourseEntity> list = new ArrayList<>();
for (Map.Entry<String, UserCourseEntity> entry : userCourses.entrySet()) {
UserCourseEntity userCourseEntityValue = entry.getValue();
String key = entry.getKey();
boolean isExist = userCourseEntityValue.getUserId().equalsIgnoreCase(userId);
if (!isExist) {
continue;
} else {
if (userCourseEntityValue.getUserId().equalsIgnoreCase(userId)) {
list.add(userCourses.get(key));
}
}
}
if (list.isEmpty()) {
logger.error("No data found");
throw new SpecificException("No data found with the given details");
}
return list;
}
We can achieve it using streams.
For that, we need to create a stream over the map-entries. Filter the entries that have values with matching userId. That transform the stream by extracting the value from each entry and collect them into a list.
Note: there's no way to throw an exception from inside the stream, hence if-statement responsible for that remains on its place.
That's how it can be implemented:
public List<UserCourseEntity> getUserCoursesByUserID(String userId) {
List<UserCourseEntity> courses = userCourses.entrySet().stream()
.filter(entry -> entry.getValue().getUserId().equalsIgnoreCase(userId))
.map(Map.Entry::getValue)
.collect(Collectors.toList()); // or .toList() for Java 16+
if (courses.isEmpty()) {
logger.error("No data found");
throw new SpecificException("No data found with the given details");
}
return courses;
}
Sidenote: from the perspective of class design, it would be cleaner if you had a user object responsible for storing and manipulate information (retrieving and changing) regarding their courses.
And you can maintain a collection of user, for instance a HashMap associating id with a user. That would allow accessing a list of courses in a convenient way.
Iterating over the HashMaps entries ins't the best way of using it.

Retrieving values from nested JSON after de-serialization

I am trying to test my REST service by a JSON string from Chrome's Advanced REST Client. I have a nested JSON here. I am taking this as string and mapping it to my POJO class:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(addressString, AddressPOJO.class);
Here, addressString holds the JSON String given below
{
"location":"[{\"Asia\":[{\"India\":[{\"city\":\"Bengaluru\"}]}], [{\"India\":[{\"city\":\"Mumbai\"}]}]}]
}
My AddressPOJO has variable:
Map<String,?> location = new HashMap();
I am retrieving the values from the POJO by
Map<String, ?> locations = addressPOJO.getLocation();
Iterator iterator1 = locations.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry pair1 = (Map.Entry)iterator1.next();
Map<String,?> cities = (Map<String,?>) pair1.getValue();
Iterator iterator2 = dataSets.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry pair2 = (Map.Entry)iterator2.next();
Map<String,?> city = (Map<String, ?>) pair2.getValue();
}
}
Here, I am only able to retrieve the second entry which is
[{\"India\":[{\"city\":\"Mumbai\"}]}]
I need to retrieve all the entries. I also tried to use MultiMap like this
MultiMap cities = (MultiMap) pair1.getValue();
But this is not accepted by compiler. Please note all the entries are dynamic in nature and the (key, value) pairs change as per user's input. Any suggestions how I can retrieve all the entries in this example.
From my understanding, maybe there are 2 things that you need to look into:
Why the data type of location is Map<String, ?>? Because from your JSON string, the type of location is an Array or a List, right? If you want to make it a Map, please use some strings like: {"location" : "\"key\":\"value\""}. If you want to make it a List, remove the "" around the value.
Another thing is that, it seems that you want a hierarchy to describe the some geography structure. Let's say, in Asia we have India and China, and in India we have Bengaluru and in China we have the city Chengdu. So the value of Asia should also be a List which contains two items India and China. So you should remove the ] and [ here, and I think this is also the reason why you are only able to retrieve the second entry.
Following is my testing code, I modified your JSON string and the data type of location.
Location.java
public class Location {
private List location;
public List getLocation() {
return location;
}
public void setLocation(final List location) {
this.location = location;
}
}
TestJSON.java
public class testJson {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(final String[] args) throws JsonParseException, JsonMappingException, IOException {
final String locationString = "{\"location\":[{\"Asia\":[{\"India\":[{\"city\":\"Bengaluru\"}]}, {\"India\":[{\"city\":\"Mumbai\"}]}]}]}";
final Location location = mapper.readValue(locationString, Location.class);
System.out.println("finish");
}
}
Then all entries and levels are ok. Maybe you can have a try.
Hope this will help.

Parsing dynamic JSON values to Java objects

In my application I have lot of overviews (tables) with sorting and filtering capabilities. And becuase the different column can hold different value type (strings, numbers, dates, sets, etc.) the filter for these columns also can bring different values. Let me show you few examples (converted to JSON already as is sent to server via REST request):
For simple string value it is like:
{"<column_name>":"<value>"}
For number and date column the filter looks like:
{"<column_name>":[{"operator":"eq","value":"<value>"}]}
{"<column_name>":[{"operator":"eq","value":"<value1>"},{"operator":"gt","value":"<value2>"}]}
For set the filter looks like
{"<column_name>":["<value1>","<value2>"(,...)]}
Now I need to parse that JSON within a helper class that will build the WHERE clause of SQL query. In PHP this is not a problem as I can call json_decode and then simply check whether some value is array, string or whatever else... But how to do this simply in Java?
So far I am using Spring's JsonJsonParser (I didn't find any visible difference between different parsers coming with Spring like Jackson, Gson and others).
I was thinking about creating an own data object class with three different constructors or having three data object classes for all of the three possibilities, but yet I have no clue how to deal with the value returned for column_name after the JSON is parsed by parser...
Simply looking on the examples it gives me three possibilities:
Map<String, String>
Map<String, Map<String, String>>
Map<String, String[]>
Any idea or clue?
Jackson's ObjectMapper treeToValue should be able to help you.
http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html#treeToValue%28com.fasterxml.jackson.core.TreeNode,%20java.lang.Class%29
Your main problem is that the first version of you JSON is not the same construction than the two others. Picking the two others you could deserialize your JSON into a Map<String, Map<String, String> as you said but the first version fits a Map.
There are a couple solutions available to you :
You change the JSON format to always match the Map<String, Map<String, String> pattern
You first parse the JSON into a JsonNode, check the type of the value and deserialize the whole thing into the proper Map pattern.
(quick and dirty) You don't change the JSON, but you try with one of the Map patterns, catch JsonProcessingException, then retry with the other Map pattern
You'll have to check the type of the values in runtime. You can work with a Map<String, Object> or with JsonNode.
Map<String, Object>
JsonParser parser = JsonParserFactory.getJsonParser();
Map<String, Object> map = parser.parseMap(str);
Object filterValue = filter.get("<column_name>");
if (filterValue instanceof String) {
// str is like "{\"<column_name>\":\"<value>\"}"
} else if (filterValue instanceof Collection) {
for (Object arrayValue : (Collection<Object>) filterValue) {
if (arrayValue instanceof String) {
// str is like "{\"<column_name>\":[\"<value1>\",\"<value2>\"]}"
} else if (arrayValue instanceof Map) {
// str is like "{\"<column_name>\":[{\"operator\":\"eq\",\"value\":\"<value>\"}]}"
}
}
}
JsonNode
ObjectMapper mapper = new ObjectMapper();
JsonNode filter = mapper.readTree(str);
JsonNode filterValue = filter.get("<column_name>");
if (filterValue.isTextual()) {
// str is like "{\"<column_name>\":\"<value>\"}"
} else if (filterValue.isArray()) {
for (JsonNode arrayValue : filterValue.elements()) {
if (arrayValue.isTextual()) {
// str is like "{\"<column_name>\":[\"<value1>\",\"<value2>\"]}"
} else if (arrayValue.isObject()) {
// str is like "{\"<column_name>\":[{\"operator\":\"eq\",\"value\":\"<value>\"}]}"
}
}
}

OrientDB serialize a query result directly to Json

I have a orient db query and want to pass the result back as json. So I have the following code:
OSQLSynchQuery<ODocument> q = new OSQLSynchQuery<ODocument>(query);
List<ODocument> result = db.command(q).execute();
return new ObjectMapper().writeValueAsString(result);
But since the serialization of ODocument leads to an infinite recursion (see this SO question) I have a problem.
Looping the list and concatinating ODocument#toJSON() to a StringBuffer is not my prefered option. Especially since I also have a case where I transform a "group by" result into a hashmap which I want to be json too. So is there a clean way on json serializing ODcomument as part of another object (list or map)?
This is for sure not a very nice way but this is working quite well. First I loop through the result and build a list of json strings:
List<String> jsonResult = new ArrayList<>();
for (ODocument d : queryResult) jsonResult.add(d.toJSON());
Next I have made a custom serializer which is just writing the raw string (json) to the buffer:
public class RawJsonListJacksonSerializer extends JsonSerializer<List<String>> {
#Override
public void serialize(List<String> entries, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartArray();
for (int i=0; i<entries.size(); i++) {
jsonGenerator.writeRaw(entries.get(i) == null ? "null" : entries.get(i));
if (i<entries.size()-1) jsonGenerator.writeRaw(",");
}
jsonGenerator.writeEndArray();
}
}

How to edit Hibernate Search java Source Code, or maybe a workaround?

I am trying to add FullTextFilters to my FullTextQuery in hibernate and there is only the method FullTextFilter.setParameter(String name, Object value)
I am trying to make a flexible, generic function to add filters to the query based on the entity its searching for, some have one parameter, some have two for their filters, so I would like to add a method to FullTextFilterImpl; setParameters(String[] names, String[] value) where I can pass in the names of all the parameters and probably a multidimensional array of the values for each parameter to transform my current code of
If( "checking which entity it is"){
fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1").setParameter("secondFilter", "val2");
}
else if("this entity's filter only has one parameter"){
fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1");
}
I tried creating a subclass of FullTextFilterImpl and putting a setParameters function in it, but the way this code is set up I'm not sure how to utilize it as FullTextQuery.enableFullTextFilter(filterName) returns a FullTextFilter object and then you call the setParameter() on that object. I'm not sure how I would get in the middle of that to do a setParameters
EDIT: I have downloaded the hibernate-search source code and added the following method to FullTextFilterImpl which I think will do what I want, but when I go to build it (even just the out-of-the-box project) I get all these checkstyle Only one new line is allowed at the end of a file errors. Is there something I'm missing from the hibernate quick-build guide.
public FullTextFilter setParameters(Map<String, List<String>> params){
for (String key : params.keySet()) {
List<String> values = params.get(key);
for(int i=0; i< values.size() ; i++){
parameters.put(key, values.get(i));
}
}
return this;
}
You can easily pass a Map of attributes to your custom Filter, the signature is:
FullTextFilter setParameter(String name, Object value);
so you could do
filter.setParameter( "myMap", properties );
where properties is an hashmap.
About the compilation error message:
Only one new line is allowed at the end of a file
is a message from checkstyle, it verifies code style is conforming to the Hibernate code style.
It's very simple to fix: there are multiple empty lines at the end of the source file, delete them. The error message should tell you what file needs to be polished.
if i correctly understand you question you need Builder pattern
here an example you could use :
public class FullTextFilter {
String[] keys;
Object[] objects;
private FullTextFilter(String[] keys, Object[] objects) {
}
public static FullTextFilterBuilder builder(){
return new FullTextFilterBuilder();
}
public static class FullTextFilterBuilder {
private Map<String, Object> parameters = new HashMap<String, Object>();
public FullTextFilterBuilder setParameter(String key, Object value){
parameters.put(key, value);
return this;
}
public FullTextFilter build(){
return new FullTextFilter(parameters.keySet().toArray(new String[0]), parameters.values().toArray(new Object[0]));
}
}
}
and then using it like this :
FullTextFilter filter = FullTextFilter.builder().setParameter("", new Object()).setParameter("", new Object()).build();
tell if that's what you are looking for.
if not i'll delete my answer
I presume you want this:
fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1").setParameter("secondFilter", "val2");
fullTextQuery{
name:"FilterName"
,parameters:["filter1":"value1", "filter2":"value2"]
}
static FullTextQuery enableFullTextFilter(String name){...}
FullTextQuery setParameter(String key, String value){
parameters.put(key, value);
return this;
}
assuming a parameters hashmap.
seeing as I was a little off base.. cant you do something like this?
setFilters (HashMap<String, String> filters) {
FullTTextFilter fl = FullTextQuery.enableFullTextFilter("filtername");
for (String key : filters.keySet()) {
fl.setParameter(key, filters.get(key));
}
}

Categories