Having a JSON with a property like this which i try to deserialze with jackson:
{ "$fooid" : "yfdfjjhkjhkljhd768622323jjj" }
Problem here is, i'm not able to read the key "$fooid".
I tried to annotate the property with
#JsonAlias("$fooid")
private String fooid;
also as
#JsonProperty("$fooid")
private String fooid;
Both variants won't work, the property fooid is always null.
How to deserialize it in Java? Any hints?
I am not sure how are you doing It, but It is working for me on version 2.0.0 doing this.
public class JSonAliasWithSpecialCharacters {
public static void main(String[] args) throws IOException {
String json="{ \"$fooid\" : \"yfdfjjhkjhkljhd768622323jjj\" }";
ObjectMapper mapper = new ObjectMapper();
JsonFooid fooid = mapper.readValue(json, JsonFooid.class);
System.out.println("read the foodid:"+fooid.getFooid());
}
}
public class JsonFooid {
#JsonProperty("$fooid")
private String fooid;
public String getFooid() {
return fooid;
}
public void setFooid(String fooid) {
this.fooid = fooid;
}
}
Related
I need to serialize/deserialize a POJO with enum. I have the following DTO:
public enum MyEnum {
VAL1("val1"),
VAL2("val2") {
#Override
public String getValue() {
return "test2";
}
};
private final String name;
MyEnum(String name) {
this.name = name;
}
public String getValue() {
return name;
}
}
public class MyPojo {
public MyEnum prop;
}
public static void main(String... args) {
Gson gson = new GsonBuilder().registerTypeAdapter(MyEnum.class, new MyEnumSeserializer());
MyPojo p = new MyPojo();
p.prop = MyEnum.VAL2; // and I get MyEnum$1.class and My serializer doesn't work
String json = gson.toJson(p);
MyPojo p1 = gson.fromJson(json, MyPojo.class);
}
How can I write a custom serializer/deserializer for proxy classes using Gson library? I can't use another library.
I've been found the solution. Need to change
new GsonBuilder().registerTypeAdapter(MyEnum.class, new MyEnumSeserializer());
to
new GsonBuilder(). registerTypeHierarchyAdapter(MyEnum.class, new MyEnumSeserializer());
and all work fine.
I want to parse JSON String into POJO class but I am getting error.
And
can we return two values from a method in java
String data
String PostcreatedMessage = "..."; // see JSON value below
{
"home_page":"/desk",
"message":"Logged In",
"full_name":"Shoaib Shaikh"
}
Main.class
LoginR loginR=new LoginR();
ObjectMapper mapper=new ObjectMapper();
loginR=mapper.readValue(PostcreatedMessage,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
parsejacker.class
public class parsejacker
{
LoginR loginR=null;
String jsonurl;
public parsejacker(String jsonurl) {
super();
this.jsonurl = jsonurl;
}
public void ParseLogin() throws JsonParseException,JsonMappingException,IOException
{
System.out.println(jsonurl+"this parselogin");
ObjectMapper mapper=new ObjectMapper();
loginR=mapper.readValue(jsonurl,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
}
}
LoginR.class
public class LoginR
{
private String homePage;
private String message;
private String fullName;
public String getHomePage(){
return homePage;
}
public void setHomePage(String input){
this.homePage = input;
}
public String getMessage(){
return message;
}
public void setMessage(String input){
this.message = input;
}
public String getFullName(){
return fullName;
}
public void setFullName(String input){
this.fullName = input;
}
}
I am Getting these errors.
(I think the error is in the PostcreatedMessage string.
How to overcome "home_page":"/desk"? --> this backslash error i.e delimiting or ignore this char)
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
Unrecognized field "home_page" (Class org.greenshoaib.greenshaikh.login.model.LoginR), not marked as ignorable
at [Source: java.io.StringReader#4738a206; line: 1, column: 15]
(through reference chain: org.greenshoaib.greenshaikh.login.model.LoginR["home_page"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at org.greenshoaib.greenshaikh.rest.client.RestAPIClient.main(RestAPIClient.java:72 )
The problem is that your JSON contains a field "home_page" but in your pojo it's called homePage (same for "full_name"). Jackson doesn't know the two are equal so you need to help it, e.g. by adding #JsonProperty("home_page") to homePage, i.e. like this (and remember to do that same for fullName):
#JsonProperty("home_page")
private String homePage;
Try your method with the following json String :
PostcreatedMessage {
"homePage":"/desk",
"message":"Logged In",
"fullName":"Shoaib Shaikh"
}
Your problem here is that the ObjectMapper cannot recognize the properties of your LoginR class by name, e.g. homePage != home_page.
You can annotate those properties to match their expected JSON key.
Here's a minimal example:
public class LoginR {
#JsonProperty(value="home_page")
private String homePage;
#JsonProperty(value="full_name")
private String fullName;
// etc. the rest of your POJO
}
... somewhere else...
String json = "{\"home_page\":\"/desk\",\"message\":\"Logged In\",\"full_name\":\"Shoaib Shaikh\"}";
ObjectMapper mapper=new ObjectMapper();
LoginR loginR = mapper.readValue(json,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
Output
/desk
Logged In
Shoaib Shaikh
I am using Jackson to deserialize a JSON string into an enum.
public enum RoomType {
SHARED("shared"),
PRIVATE("private");
private String value;
RoomType(String value) {
this.value = value;
}
#JsonCreator
public static RoomType fromJson(final String jsonValue) {
for (RoomType type : values()) {
if (type.value.equals(jsonValue)) {
return type;
}
}
return null;
}
#JsonValue
#Override
public String toString() {
return value;
}
}
I want to unit test the different edge cases:
#RunWith(JUnit4.class)
public class RoomTypeTest {
private final ObjectMapper mapper = new ObjectMapper();
#Test
public void fromJsonWithShared() throws Exception {
String json = "{\"roomType\":\"shared\"}";
RoomType type = mapper.readValue(json, RoomType.class);
assertThat(type).isEqualTo(RoomType.SHARED);
}
}
The test fails. When I debug I see that jsonValue is null when RoomType.fromJson is invoked. Seems like that Jackson does not pick up the value from the JSON string.
Related examples
EnumCreatorTest929.java
I think Jackson doesn't know what value to pass to that fromJson method. Try adding #JsonProperty:
#JsonCreator
public static RoomType fromJson(#JsonProperty("roomType") final String jsonValue) {
....
}
I'm looking for possibility to serialize transient information only in some cases:
#JsonInclude(Include.NON_NULL)
#Entity
public class User {
public static interface AdminView {}
... id, email and others ...
#Transient
private transient Details details;
#JsonIgnore // Goal: ignore all the time, except next line
#JsonView(AdminView.class) // Goal: don't ignore in AdminView
public Details getDetails() {
if (details == null) {
details = ... compute Details ...
}
return details;
}
}
public class UserDetailsAction {
private static final ObjectWriter writer = new ObjectMapper();
private static final ObjectWriter writerAdmin = writer
.writerWithView(User.AdminView.class);
public String getUserAsJson(User user) {
return writer.writeValueAsString(user);
}
public String getUserAsJsonForAdmin(User user) {
return writerAdmin.writeValueAsString(user);
}
}
If I call getUserAsJson I expected to see id, email and other fields, but not details. This works fine. But I see same for getUserAsJsonForAdmin, also without detail. If I remove #JsonIgnore annotation - I do see details in both calls.
What do I wrong and is there good way to go? Thanks!
You may find the use of the dynamic Jackson filtering slightly more elegant for your use case. Here is an example of the filtering of POJO fields based on a custom annotation sharing one object mapper instance:
public class JacksonFilter {
static private boolean shouldIncludeAllFields;
#Retention(RetentionPolicy.RUNTIME)
public static #interface Admin {}
#JsonFilter("admin-filter")
public static class User {
public final String email;
#Admin
public final String details;
public User(String email, String details) {
this.email = email;
this.details = details;
}
}
public static class AdminPropertyFilter extends SimpleBeanPropertyFilter {
#Override
protected boolean include(BeanPropertyWriter writer) {
// deprecated since 2.3
return true;
}
#Override
protected boolean include(PropertyWriter writer) {
if (writer instanceof BeanPropertyWriter) {
return shouldIncludeAllFields || ((BeanPropertyWriter) writer).getAnnotation(Admin.class) == null;
}
return true;
}
}
public static void main(String[] args) throws JsonProcessingException {
User user = new User("email", "secret");
ObjectMapper mapper = new ObjectMapper();
mapper.setFilters(new SimpleFilterProvider().addFilter("admin-filter", new AdminPropertyFilter()));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
shouldIncludeAllFields = true;
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
}
}
Output:
{
"email" : "email"
}
{
"email" : "email",
"details" : "secret"
}
It's look like jackson have horrible concept on very cool feature like #JsonView. The only way I discover to solve my problem is:
#JsonInclude(Include.NON_NULL)
#Entity
public class User {
public static interface BasicView {}
public static interface AdminView {}
... id and others ...
#JsonView({BasicView.class, AdminView.class}) // And this for EVERY field
#Column
private String email;
#Transient
private transient Details details;
#JsonView(AdminView.class)
public Details getDetails() {
if (details == null) {
details = ... compute Details ...
}
return details;
}
}
public class UserDetailsAction {
private static final ObjectWriter writer = new ObjectMapper()
.disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
.writerWithView(User.BasicView.class);
private static final ObjectWriter writerAdmin = new ObjectMapper()
.disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
.writerWithView(User.AdminView.class);
public String getUserAsJson(User user) {
return writer.writeValueAsString(user);
}
public String getUserAsJsonForAdmin(User user) {
return writerAdmin.writeValueAsString(user);
}
}
Maybe it's help some one. But I hope to find better solution and because doesn't accept my own answer.
EDIT: because interface can extends (multiple) interfaces, I can use:
public static interface AdminView extends BasicView {}
and just
#JsonView(BasicView.class)
instead of
#JsonView({BasicView.class, AdminView.class})
I have JSON, with differents levels field, so I want to convert to a single JSON with fields with one level for example:
{
"prop1":"value1",
"prob2":"value2",
"prop3": {
"prop4":"value4",
"prop5":"value5"
}
... many level fields
}
result
{
"prop1":"value1",
"prop2":"value2",
"prop4":"value4",
"prop5":"value5"
.......
}
I'm using Jackson with annotation #JsonProperty("field"), I haven't problem wih fields of first level , but I donĀ“t know how to access field where to into more inside JSON , for this example are prop4 and prop5.
JsonUnwrapped is the annotation to use, it even works for multi-level nesting. For example:
#RunWith(JUnit4.class)
public class Sample {
#Test
public void testName() throws Exception {
SampleClass sample = new SampleClass("value1", "value2", new SubClass("value4", "value5", new SubSubClass("value7")));
new ObjectMapper().writeValue(System.out, sample);
}
#JsonAutoDetect(fieldVisibility=Visibility.ANY)
public static class SampleClass {
private String prop1;
private String prop2;
#JsonUnwrapped
private SubClass prop3;
public SampleClass(String prop1, String prop2, SubClass prop3) {
this.prop1 = prop1;
this.prop2 = prop2;
this.prop3 = prop3;
}
}
#JsonAutoDetect(fieldVisibility=Visibility.ANY)
public static class SubClass {
private String prop4;
private String prop5;
#JsonUnwrapped
private SubSubClass prop6;
public SubClass(String prop4, String prop5, SubSubClass prop6) {
this.prop4 = prop4;
this.prop5 = prop5;
this.prop6 = prop6;
}
}
#JsonAutoDetect(fieldVisibility=Visibility.ANY)
public static class SubSubClass{
private String prop7;
public SubSubClass(String prop7) {
this.prop7 = prop7;
}
}
}
will generate
{"prop1":"value1","prop2":"value2","prop4":"value4","prop5":"value5","prop7":"value7"}
Try implementing the #JsonUnwrapped annotation. More information at http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/annotate/JsonUnwrapped.html