Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON field mapping capitalization?

Tags:

java

json

jackson

I'm not clear how jackson deals with capitalization in mapping fields. If anyone could help I'd appreciate it.

{"user":{"username":"[email protected]","password":"pwd","sendercompid":"COMPID","service":{"host":"address","port":6666,"service":"S1","serviceAsString":"s1"}},"MDReqID":"ghost30022","NoRelatedSym":1,"Symbol":["GOOG"],"MarketDepth":"0","NoMDEntryTypes":3,"MDEntryType":["0","1","2"],"SubscriptionRequestType":"1","AggregatedBook":"N"}: 

Above is my json, below is my exception...

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "MDReqID" (class com.myco.qa.fixrest.MarketDataRequest), not marked as ignorable (10 known properties: , "mdreqID", "marketDepth", "user", "subscriptionRequestType", "aggregatedBook", "mdentryType", "symbol", "mdupdateType", "noRelatedSym", "noMDEntryTypes"]) 

Above is my exception, below is my class...

public class MarketDataRequest {     private User user;     private String MDReqID;     private char SubscriptionRequestType;     private int MarketDepth;     private int MDUpdateType;     private char AggregatedBook;     private int NoMDEntryTypes;     private ArrayList<Character> MDEntryType;     private int NoRelatedSym;     private ArrayList<String> Symbol;      public User getUser() {         return user;     }      public void setUser(User user) {         this.user = user;     }      public String getMDReqID() {         return MDReqID;     }      public void setMDReqID(String MDReqID) {         this.MDReqID = MDReqID;     }      public char getSubscriptionRequestType() {         return SubscriptionRequestType;     }      public void setSubscriptionRequestType(char subscriptionRequestType) {         SubscriptionRequestType = subscriptionRequestType;     } 

... et cetera

like image 762
shaz Avatar asked Mar 08 '13 20:03

shaz


1 Answers

Since your setter method is named setMDReqID(…) Jackson assumes the variable is named mDReqID because of the Java naming conventions (variables should start with lower case letters).

If you really want a capital letter use the @JsonProperty annotation on the setter (or - for serialization - on the getter) like this:

@JsonProperty("MDReqID") public void setMDReqID(String MDReqID) {     this.MDReqID = MDReqID; } 
like image 163
nutlike Avatar answered Sep 18 '22 03:09

nutlike