Jackson serialise fields as uppercase? - json

My object gets serialised as {name="nyname" ....}, however, I require all keys to be start with uppercase, ie. {Name="myname",.....} how can I do that with Jackson ?

I had the same issue and posted about it earlier. I solved it here:
How can I enable Pascal casing by default when using Jackson JSON in Spring MVC?
If you're not using Spring, the answer would be similar. You'll still need a custom (de)serializer, and you'd just need to wire it in to your program in the manner you choose.

Related

How to generate Recordio from Java object

I am trying to serialize a list of java Objects (POJO) into RecordIO format. I have seen this BeanIO (http://beanio.org/) but it seems to be outdated. Is there any other Java library that could be used or a different way to do this ?
Once list of objects is serialized it will be used to train a model with SageMaker.
Solving my own problem. I decided to use Apache Avro instead of BeanIO. Spark allow to serialize using Avro (c.f. Spark-Avro). This seems to work however it did not fit my use case has I was trying to serialize an array of numbers.

How to apply low-level character filter to Jackson JSON parsing?

I have a (JSON) REST-based application based on Spring and RESTEasy. We have a requirement to limit the set of characters that are accepted as input to the services. I could apply the restriction to the POJOs themselves during or after deserialization, but it seems more efficient to insert some kind of filter into the Jackson parsing as it's reading the JSON stream, since it's obviously inspecting each character at some point anyway.
Question is, does Jackson provide a plug-in point to do that? Something that would enable me to decorate or override the low-level parsing at such a time that I could verify each character of each property value is valid (i.e., in a particular set according to the app requirements).
If you mean allowed characters within JSON String values, no, there is no filter or transformer functionality currently. You could consider implementing JsonParserDelegate, which could intercept calls to getText(), but that may not be very clean mechanism. Alternatively you could first read content as tree (JsonNode), traverse it and cleanse String values; and only after that do data-binding using ObjectMapper.convertValue(fromNode, MyType.class).
In the context of RESTEasy, you can inject the custom JsonParserDelegate by extending MappingJsonFactory and overriding the _createJsonParser(...) methods to return the custom delegate. Then implement an extension to JacksonJsonProvider (Jackson's JAX-RS provider) that creates an ObjectMapper with your custom factory in its constructor. Examples of this can be found in this answer to a related question.

How to have Jackson deserialize a cyclical graph that has been run through JSOG.stringify(myCyclicalGraph)

I am currently using the this Jackson plugin
Which serialized my cyclical graphs. Then on the client I use the JSOG to decode the {#ref} objects like this:
JSOG.decode(data)
The problem comes when I am trying to send the json back up to the server. If I don't do anything to the data I get a "Maximum call stack size exceeded", obviously because my js object is cyclical. I try using:
JSOG.stringify(data);
But then Jackson chokes on all the #id and #refs:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "#id"
Has any one figured out how to do this?
Both sides need to be able to support JSOG convention. It is not part of JSON specification, nor can it be automatically detected; so Jackson can not process it without help.

Parsing Jackson annotations with Swagger

By default, Swagger parses a class's data members in order to document the objects used as parameters or returned by a given web service. If you're using Jackson, the Jackson annotations provide a much more accurate description of the API.
Does anyone know an (easy) way to get Swagger to parse Jackson annotations. Perhaps an overridden parser?
Not sure if this would help, but Jackson 2.1 and later expose POJO structure as seen by Jackson itself (ObjectMapper.acceptJsonFormatVisitor), which could be used for generating different kinds of artifacts. I have written an Avro schema generator with it, for example (as part of Jackson Avro module)
As of version 1.2, Swagger can parse Jackson annotations on its own. I confirmed this using Jackson 2.1.

JAXB A cycle is detected in the object graph

I want to convert my pojo to json with JAXB, my pojo have one to many relation, and when i convert my pojo to json, JAXB generate error "A cycle is detected in the object graph. This will cause infinitely deep XML".
I read from web that, this problem can be solved with help from #XmlID and #XmlIDREF, but there is one problem, my Id attribute is not String type but Long. and as far as i know #XmlID can be used only with String property.
Other web suggest using eclipselink MOXy, but MOXy cannot generate json.
As you mentioned in your question EclipseLink MOXy (I'm the tech lead) has the #XmlInverseReference annotation to solve the problem of bidirectional relationships. As of EclipseLink 2.4 MOXy can produce/consume JSON.
For More Information
http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
You have a cyclic reference problem in your definition.
Try putting #XmlTransient above the problematic definition.
Also, about XmlID and string type, see http://markmail.org/message/up6vrzjixxrvy5th.
The JAXB specification requires that the property marked with #XmlID be a String property.
MOXy impl allows to use long.
One hack to keep using full JAXB compliant implementation would be to duplicate your id in a String field (before serialising)
Don't know so much about JAXB but XStream makes you able to use different modes and some of these modes will give references to the xpath address (absolute or relative) of an element in your xml, if these elements are already displayed.
(And you can do Json with XStream)
I faced similar problem when I wanted to convert my POJO to JSON with JaxRS. The MoxyJsonProvider is the default option of eclipselink but it fails to parse the JSOG (where cycles exists in the JSON structure). Jackson Jaxb Provider does this better with ObjectMapper.
I have elaborated in this answer below, about how to invoke Jackson Provider instead of Moxy. You will need jackson packages in your pom xml.
https://stackoverflow.com/a/60319306/5076414