// to enable standard indentation ("pretty-printing"):
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// to allow serialization of "empty" POJOs (no properties to serialize)
// (without this setting, an exception is thrown in those cases)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// to write java.util.Date, Calendar as number (timestamp):
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// DeserializationFeature for changing how JSON is read as POJOs:
// to prevent exception when encountering unknown property:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// to allow coercion of JSON empty String ("") to null Object value:
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// to allow C/C++ style comments in JSON (non-standard, disabled by default)
mapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
// to allow (non-standard) unquoted field names in JSON:
mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
// to allow use of apostrophes (single quotes), non standard
mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
// JsonGenerator.Feature for configuring low-level JSON generation:
// to force escaping of non-ASCII characters:
mapper.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
格式化输出
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JackjsonCfg {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// to enable standard indentation ("pretty-printing"):
System.out.println("----------------------格式化输出-------------------");
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Map<String, String> mapJson = new HashMap<String, String>();
mapJson.put("nameLove", "zhangs");
mapJson.put("nameLove1", "lis");
String sjson = mapper.writeValueAsString(mapJson);
System.out.println(sjson);
}
}
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// to allow serialization of "empty" POJOs (no properties to serialize)
// (without this setting, an exception is thrown in those cases)
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// to write java.util.Date, Calendar as number (timestamp):
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// DeserializationFeature for changing how JSON is read as POJOs:
// to prevent exception when encountering unknown property:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// to allow coercion of JSON empty String ("") to null Object value:
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// to allow C/C++ style comments in JSON (non-standard, disabled by default)
mapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
// to allow (non-standard) unquoted field names in JSON:
mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
// to allow use of apostrophes (single quotes), non standard
mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
// JsonGenerator.Feature for configuring low-level JSON generation:
// to force escaping of non-ASCII characters:
mapper.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
格式化输出
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JackjsonCfg {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// to enable standard indentation ("pretty-printing"):
System.out.println("----------------------格式化输出-------------------");
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Map<String, String> mapJson = new HashMap<String, String>();
mapJson.put("nameLove", "zhangs");
mapJson.put("nameLove1", "lis");
String sjson = mapper.writeValueAsString(mapJson);
System.out.println(sjson);
}
}