Fast is not the only factor to be considered. Compared with database IO, json parsing time is negligible.
2 fastjson has made many optimizations for specific requirements, resulting in less stringent checks, such as array [daylight saving time], summer time, or not at the end.
3. Jackson is more powerful and more configurable (fastjson)
For example, serialize guava types, serialize time and date in Java 8, and so on. Common configurations are as follows
//When serializing BigDecimal, the original number is output or the scientific count is output. The default is false, that is, whether to Plain String () scientific count is output or not. objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); //Allow JSON empty strings to be coerced into null object values objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); //Allow single values to be processed as arrays objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); //No duplicate keys, throw exceptions objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY); //Disallow using order() of int for Enum to deserialize Enum and throw exceptions objectMapper.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS); //Do not report errors when there are attributes that cannot be mapped objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); //Use null to indicate that a collection type field is not throwing an exception objectMapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); //Object is space-time and no exception is thrown objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); //Allow the use of c/c++ style annotations in JSON objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS); //Mandatory escape of non-ascii characters objectMapper.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII); //Allow unknown fields objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN); //Allow unreferenced field names in JSON objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES); //Time format objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); //Identify single quotation marks objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); //Recognition of Special Characters objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS); //Recognizing Java 8 time objectMapper.registerModule(new ParameterNamesModule()); objectMapper.registerModule(new Jdk8Module()); objectMapper.registerModule(new JavaTimeModule()); //Classes that identify Guava packages objectMapper.registerModule(new GuavaModule());
Setting different name s
// without annotation, we'd get "theName", but we want "name": @JsonProperty("name") public String getTheName() { return _name; }
Ignore attributes
// means that if we see "foo" or "bar" in JSON, they will be quietly skipped // regardless of whether POJO has such properties @JsonIgnoreProperties({ "foo", "bar" }) public class MyBean { // will not be written as JSON; nor assigned from JSON: @JsonIgnore public String internal;
4 fastjson has some controversial implementations and poor code quality. https://pic3.zhimg.com/80/v2-...
fastjson may have memory leaks below jdk1.7 because of the use of substring, while JDK1.6 substring has keneng with memory leaks
Reference resources: https://www.programcreek.com/...
//JDK 6 String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; } public String substring(int beginIndex, int endIndex) { //check boundary return new String(offset + beginIndex, endIndex - beginIndex, value); } //JDK 7 public String(char value[], int offset, int count) { //check boundary this.value = Arrays.copyOfRange(value, offset, offset + count); } public String substring(int beginIndex, int endIndex) { //check boundary int subLen = endIndex - beginIndex; return new String(value, beginIndex, subLen); }
5 Gson can handle three-tier nesting of json
Gson can automatically handle the three-tier nesting of json, for example, but both jackson and fast JSON are troublesome to handle (but JSON does not recommend multi-tier nesting in fact)
class C { B b; } class B{ List<A> a } Class A{ }
jackson's approach to two-tier nesting: rewriting deserialization
public class Order { public String name; public List<Item> items; } class ItemsJsonDeserializer extends JsonDeserializer<List<Item>> { @Override public List<Item> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { InnerItems innerItems = jp.readValueAs(InnerItems.class); return innerItems.elements; } private static class InnerItems { public List<Item> elements; } } public class Order { public String name; @JsonDeserialize(using = ItemsJsonDeserializer.class) public List<Item> items; }
6. Jackson has a broader community base