I have some JSON as below
{
"FName": "Test",
"LName": "Test"
}
When i use json2pojo maven plugin(0.4.37), the POJOs are created with the proper Java naming conventions(like fName,lName). Is there a way to override this and create the POJOs with the same names (like FName, LName).
Annotate your fields with #JsonProperty() annotation
import com.fasterxml.jackson.annotation.JsonProperty;
class SomeClass {
#JsonProperty("FName")
private String fName;
#JsonProperty("LName")
#private String lName;
//getters and setters
}
Related
I have to create pojos from graphql schema file into java. like for
type Student{
name:String
age:Int
}
i created pojo
class Student{
private String name;
private int age;
// setter and getters
}
how to handle interface in java classes like
interface prop{
alttext: String
description: String
linkurl: String
title: String
}
I am writing a work around for my question, might be best solution is some thing else-
i created abstract class in java from interface of graphql schema. like below -
public abstract class prop {
private String alttext;
private String description;
private String linkurl;
private String title;
}
and for class extending this abstract class use super in constructor in child class to initialized abstract class members.
I am trying to create a way to serialize and deserialize objects to JSON using Jackson without having to rely on annotations, mix-ins or any code that is object-specific (ie: specific deserializers). However, I'm having trouble deserializing interfaces.
An idea that I had was to store the class name of the object within the JSON. For instance, if I have the following classes:
MyClassOne.java
package test.classes;
public class MyClassOne{
private String myString;
private MyClass myReference;
public MyClassOne(String myString, MyClass myReference) {
this.myString = myString;
this.myReference = myReference;
}
public String getMyString() {
return myString;
}
public MyClass getMyReference() {
return myReference;
}
}
MyClassTwo.java
package test.classes;
public class MyClassTwo implements MyClass{
private int myInt;
public MyClassTwo(int myInt) {
this.myInt = myInt;
}
public int getMyInt() {
return myInt;
}
}
I would like to use Jackson to serialize MyObjectOne instances into something similar to:
{
"_class":"test.classes.MyClassOne" ,
"myString":"Hello World",
"myReference":{
"_class":"test.classes.MyClassTwo",
"myInt":2
}
}
I was wondering if that could work and how to accomplish it with Jackson.
An idea that I had was to store the class name of the object within the JSON.
You could consider enableDefaultTypingAsProperty() for automatic inclusion of type information:
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, "_class");
I am trying to create a way to serialize and deserialize objects to JSON using Jackson without having to rely on annotations, mix-ins or any code that is object-specific (ie: specific deserializers).
Consider the following code using the types defined in your own question:
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "_class");
MyClass myClass = new MyClassTwo(2);
MyClassOne myClassOne = new MyClassOne("Hello World", myClass);
String json = mapper.writer()
.withDefaultPrettyPrinter()
.writeValueAsString(myClassOne);
It will produce the following JSON:
{
"_class" : "com.example.examples.foo.MyClassOne",
"myString" : "Hello World",
"myReference" : {
"_class" : "com.example.examples.foo.MyClassTwo",
"myInt" : 2
}
}
To deserialize the above JSON, however, your classes must have a default constructor (or use #JsonCreator).
I have an annotation
public #interface Field {
String value();
}
and java class, annotated by it:
public class Animal {
#Field("name")
private String name;
}
I try to list all field' annotations by the next code:
for(field in clazz.declaredFields){
for(annotation in field.annotations){
when(annotation){
is Field -> {
//do something
}
}
}
}
where clazz is Class<T>
but field.annotations is empty.
How to list annotations correctly?
Java annotations, by default, are not retained at runtime so you'll need to specify such:
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
#Retention(RUNTIME)
public #interface Field {
String value();
}
Kotlin annotations are retained by default:
annotation class Field(val value: String)
The issue isn't Kotlin specific, you just haven't configured Field annotation properly. By default, each annotation is retained with RetentionPolicy.CLASS, meaning it won't be accessible via reflection. You have to use RetentionPolicy.RUNTIME if you want to access the annotation in the runtime.
#Retention(RetentionPolicy.RUNTIME)
public #interface Field {
String value();
}
I have some class with Gson annotations that I need to keep from obfuscation via ProGuard. This bit of code works
public abstract class FacebookIdentifier {
#Expose public String id;
#Expose public String name;
}
-keepclasseswithmembers class * {
#com.google.gson.annotations.* <fields>;
}
Now I have some classes that extend such classes with no extra field. Example:
class FacebookApplication extends FacebookIdentifier {}
Such a class is obfuscated even though its parent has some annotations that prevent it from being obfuscated. Is there a way to have this class not being obfuscated ?
You would have to specify the extension explicitly:
-keep class com.example.FacebookApplication
However, for JSON, the class names probably don't matter; only the field names. Preserving the fields should be sufficient:
-keepclassmembers class * {
#com.google.gson.annotations.* <fields>;
}
This is assuming that all serialized fields are annotated, which is not strictly required for GSON.
I was wondering if anyone has attempted and been successful at using the Morphia jar for interacting with a mongodb database inside of GWT? I've been using the below object as the base for all my POJO's, however whenever I attempt to save down the object using an UpdateOperations<DerivedPersistentEntity> or datastore.Save() I get a ConcurrentModificationException.
package com.greycells.dateone.shared;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Version;
public class PersistentEntity {
#Id
private String id;
#Version
private Long version = null;
public PersistentEntity() {
}
public String getId() {
return id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
I've also added the gwt extension jar that you have to download separately for Morphia and referenced it in my gwt.xml and this seems to be of no help. Additionally I've tried changing the id field of PersistentEntity to the ObjectId type but then I can't even get my project to bind correctly because it complains of...
[ERROR] No source code is available for type org.bson.types.ObjectId; did you forget to inherit a required module?
You can't use a String for the #Id field of the entity in Morphia it must be an ObjectId. GWT support in Morphia is completely broken as of v1.02.