jackson
Treatment objects and
JSON
The experience of converting to each other.
jackson
is a use
Java
Written for handling
The class library of
JSON
format data is very fast. At present, it is widely used, and it has gradually replaced
Gson
json-lib
。
If you introduce it directly
jar
package, you can access this address download
http://jackson.codehaus.org/1.9.11/jackson-all-1.9.11.jar
If used
maven
Construct a project and add the following dependence
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</
dependency
>
No code and no truth. For the simplest explanation, I directly apply the code.
public class User {
private String name;
private Gender gender;
private List<Account> accounts;
omitgetand harmonysetMethod
…
}
public enum Gender {
MALE,
FEMALE
}
public class Account {
private Integer id;
private String cardId;
private BigDecimal balance;
private Date date;
omitgetandsetMethod
…
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setName(“Pineapple Elephant“);
user.setGender(Gender.MALE);
List<Account> accounts = new ArrayList<Account>();
Account account = new Account();
account.setId(1);
account.setBalance(BigDecimal.valueOf(1900.2));
account.setCardId(“423335533434”);
account.setDate(new Date());
accounts.add(account);
account = new Account();
account.setId(2);
account.setBalance(BigDecimal.valueOf(5000));
account.setCardId(“625444548433”);
account.setDate(new Date());
accounts.add(account);
user.setAccounts(accounts);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);
String json = mapper.writeValueAsString(user);
System.out.println(“Java2Json: “+json);
user = mapper.readValue(json, User.class);
System.out.println(“Json2Java: “+mapper.writeValueAsString(user));
}
mapper.configure(SerializationConfig.Feature.
INDENT_OUTPUT
, Boolean.
TRUE
);
This is the auxiliary settings and control the formatting output.
The
mapper.getSerializationConfig().setXxx
method is now marked as
@Deprecated
, so please use the above method to deal with it.
SerializationConfig.Feature
There are many other setting items in the enumeration, such as the date, such as
Do not output
null
value and so on. Others are:
org.codehaus.jackson.JsonGenerator.Feature.*
org.codehaus.jackson.JsonParser.Feature.*
Let’s take a look at the output results. After the two conversion, the printed string should be the same:
OK
, really the result is the same, everyone should use now
jackson
Java
and
Json
The conversion of each other, right? Well, consider another situation now, if you want to
List<User>
JSON
string reverses into generics, what should I do?
Think of this:
mapper.readValue(json, List<User>.
class
)
? This is wrong, the parameter here is
Class<T> valueType
,
valueType
Yes
Class<T>
Object. As shown above
User.
class
Object of
Class<User>
Class. Therefore, if you want to get a generic collection type, you need to pass other methods:
*
Get generate
Collection Type
*
@param
jsonStr json
string
*
@param
collectionClass
generic
Collection
*
@param
elementClasses
element type
*/
public static
<T> T readJson(String jsonStr, Class<?> collectionClass, Class<?>… elementClasses)
throws
Exception {
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
return mapper.readValue(jsonStr, javaType);
}
Define one
List
<User>
, add twice to it
user
, first call
Writevalueasstring method
print out
json
, then call
readJson
Method, this can not only convert generics
List<T>
, can also be used for other sets, such as
Map<K,V>
and so on.
List<User> list =
readJson(json, List.
class
, User.
class
);
ObjectMapper
Let the object and
JSON
Convert each other, other than that
Jackson
Also provided
JsonGenerator
and
JsonParser
These two categories, they can process serialization and derivativeization of fine granularity. transfer
ObjectMapper
writeValueAsString
and
readValue
Method,
will eventually be left to
JsonGenerator
and
JsonParser
To deal with it, if you have any doubt about this, you can see the source code of these two methods.
This article is original. If you want to reprint, please indicate the source.
http://www.blogjava.net/bolo