- Jackson tutorial(http://wiki.fasterxml.com/JacksonInFiveMinutes)
- 차례
- JSON Three Ways
- Examples
- Full Data Binding (POJO) Example
- Simple Data Binding Example
- Data Binding with Generics
- Three Model Example
- Streaming API Example
- JSON Three Ways
- Jackson은 JSON 데이터를 처리하기 위한 세가지 방법을 제공한다.
- Streaming API(aka Incremental
Processing, or Token Streams)
- http://wiki.fasterxml.com/JacksonStreamingApi
- Streaming API는 JSON 데이터를 처리하는데 가장 효과적인 방법이다.
- 어플리케이션 보다는 주로 미들웨어나 프레임워크와 같이 성능이 중요한 곳에서 사용된다.
- 장점 : Lowest memory and processing overhead
- 단점
- 모든 데이터는 순차적으로 처리된다. Random Access 불가.
- 매우 기본적인 데이터 타입만 제공한다.(String, byte[] for base64-encoded binary content)
- org.codehaus.jackson.JsonParser
reads, org.codehaus.jackson.JsonGenerator writes.
- Creating Parsers
- Paraser는 JSON Content에서 각각의 데이터를 분리한다.
- JSON Content에 대한 Low-level access.
- org.codehaus.jackson.JsonFactory has extensive set of methods to construct parsers
- Example
- Creating Parsers
JsonFactory jsonFactory = new JsonFactory();
// or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory
JsonParser jp = jsonFactory.createJsonParser(file); // or URL, Stream, Reader, String, byte[]
- Creating Generators
- JSON Cotent를 JSON 각각의 데이터로 분리해준다.
- Example
JsonFactory jsonFactory = new JsonFactory();
// or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory
JsonGenerator jg = jsonFactory.createJsonGenerator(file, JsonEncoding.UTF8); // or Stream, Reader
- Configuring
- JsonParser.Features
- http://wiki.fasterxml.com/JacksonFeaturesParser
- AUTO_CLOSE_SOURCE, INTERN_FIELD_NAMES, CANONICALIZE_FIELD_NAMES
- ALLOW_COMMENTS, ALLOW_UNQUOTED_FIELD_NAMES, ALLOW_SINGLE_QUOTES, ALLOW_UNQUOTED_CONTROL_CHARS
- JsonGenerator.Feature
- http://wiki.fasterxml.com/JacksonFeaturesGenerator
- AUTO_CLOSE_TARGET, AUTO_CLOSE_JSON_CONTENT, QUOTE_NON_NUMERIC_NUMBERS, WRITE_NUMBERS_AS_STRINGS
- QUOTE_FIELD_NAMES
- JsonParser.Features
- Tree Model
- http://wiki.fasterxml.com/JacksonTreeModel
- Constructing Trees From JSON
Content
- ObjectMapper를 이용하며, 두가지 방법이있다.
- Example
<<첫번째 방법>>
// general method, same as with data binding
ObjectMapper mapper = new ObjectMapper();
// (note: can also use more specific type, like ArrayNode or ObjectNode!)
JsonNode rootNode = mapper.readValue(src, JsonNode.class); // src can be a File, URL, InputStream etc
<<두번째 방법>>
// or, with dedicated method; requires a JsonParser instance
JsonParser jp = ...; // construct using JsonFactory (can get one using ObjectMapper.getJsonFactory)
rootNode = mapper.readTree(jp);
// (most useful when binding sub-trees)
- Constructing Tree Model instances from Scratch
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.createObjectNode(); // will be of type ObjectNode
((ObjectNode) rootNode).put("name", "Tatu");
// ... and so forth
- Writing Trees as JSON
mapper.writeValue(dst, myBean); // dst can be File, OutputStream, Writer etc
- Converting to Token Stream
JsonParser jp = node.traverse();
// or:
JsonParser jp = mapper.treeAsTokens(node);
- Converting to Java Objects
MyBean bean = mapper.treeToValue(node, MyBean.class);
- Data Binding
- http://wiki.fasterxml.com/JacksonDataBinding
- converts JSON to and from POJOs based either on property accessor conventions or annotations.
- 두 가지 방법
- Simple data binding means converting to and from Java Maps, Lists, Strings, Numbers, Booleans and nulls
- Full data binding means converting to and from any Java bean type (as well as "simple" types mentioned above)
- org.codehaus.jackson.map.ObjectMapper performs the marshalling and unmarshalling for both variants.
- Serialization
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(dst, myBean); // where 'dst' can be File, OutputStream or Writer
- Full Data Binding
MyBean value = mapper.readValue(src, MyBean.class); // 'src' can be File, InputStream, Reader, String
- Generic Types: TypeReference Object를 사용한다.
MyType<String> genValue = mapper.readValue(src, new TypeReference<MyType<String>>() { });
- "Simple" Data Binding
Object root = mapper.readValue(src, Object.class);
Map<?,?> rootAsMap = mapper.readValue(src, Map.class);
- 사용 가능한 Type
JSON Type
Java Type
object
LinkedHashMap<String,Object>
array
ArrayList<Object>
string
String
number (no fraction)
Integer, Long or BigInteger (smallest applicable)
number (fraction)
Double (configurable to use BigDecimal)
true|false
Boolean
null
null
- Configuring
- SerializationConfig.Feature
- http://wiki.fasterxml.com/JacksonFeaturesSerialization
- AUTO_DETECT_GETTERS, AUTO_DETECT_FIELDS, CAN_OVERRIDE_ACCESS_MODIFIERS,
- USE_STATIC_TYPING, WRITE_DATES_AS_TIMESTAMPS, WRITE_DATES_AS_TIMESTAMPS,
- USE_ANNOTATIONS, FAIL_ON_EMPTY_BEANS, DEFAULT_VIEW_INCLUSION
- DeserializationConfig.Feature
- http://wiki.fasterxml.com/JacksonFeaturesDeserialization
- USE_ANNOTATIONS, AUTO_DETECT_SETTERS, AUTO_DETECT_CREATORS, AUTO_DETECT_FIELDS,
- USE_GETTERS_AS_SETTERS, CAN_OVERRIDE_ACCESS_MODIFIERS, USE_BIG_DECIMAL_FOR_FLOATS,
- USE_BIG_INTEGER_FOR_INTS, USE_BIG_INTEGER_FOR_INTS
- SerializationConfig.Feature
- 정리하면,
- Streaming API는 성능이 가장 좋고, Data Binding은 가장 사용하기 편하며, Tree Model은 가장 유연하다.
- 특별한 사유가 아니라면 Data Binding을 쓰겠지?;;;
'Programming > Java' 카테고리의 다른 글
Spring에서 Json으로의 입출력 정리 (0) | 2014.09.11 |
---|---|
Jackson Tutorial - 2 (0) | 2014.09.11 |
Jackson How-To: Ignoring Unknown Properties (0) | 2014.09.11 |
파싱(Parsing)강좌 (0) | 2014.05.23 |
JNI사용하기 (여기서는 java + c, 사용툴 : Visual studio) (0) | 2014.02.26 |
댓글