Share
Embed
Intro to Moshi
Moshi is an open source modern JSON library for Java/Android. I am giving an introduction to the library here.
As you can see, Moshi is intuitive and simple library having some awesome features such as lenient mode and getting path.
Here are some links in the slides.
- Moshi
https://github.com/square/moshi
- An Open Source Advantage (Droidcon MTL 2015)
https://speakerdeck.com/swankjesse/an-open-source-advantage-droidcon-mtl-2015?slide=58
- Moshi Playground repository
https://github.com/hkurokawa/MoshiPlayground
- Issue #29
https://github.com/square/moshi/issues/29
Hiroshi Kurokawa
May 13, 2015
Transcript
12 slides
-
What is Moshi https://github.com/square/moshi A modern JSON library for Android
and Java. Developed by Square (Jake and Jesse) Simple Easy to use Take advantage of okio -
Before showing code Visit https://github.com/hkurokawa/MoshiPlayground All the sample code in
the presentation is there. -
Read and map JSON to a POJO How to use
it @ T e s t p u b l i c v o i d t e s t P O J O ( ) { f i n a l S t r i n g j s o n = " { \ " n a m e \ " : \ " J a k e W h a r t o n \ " , \ " a g e \ " : 3 1 } " ; f i n a l M o s h i m o s h i = n e w M o s h i . B u i l d e r ( ) . b u i l d ( ) ; f i n a l U s e r u s e r = m o s h i . a d a p t e r ( U s e r . c l a s s ) . f r o m J s o n ( j s o n ) ; } p u b l i c s t a t i c c l a s s U s e r { p r i v a t e S t r i n g n a m e ; p r i v a t e i n t a g e ; p u b l i c S t r i n g g e t N a m e ( ) { r e t u r n n a m e ; } p u b l i c v o i d s e t N a m e ( S t r i n g n a m e ) { t h i s . n a m e = n a m e ; } -
How to use it Specify a particular JSON adapter @
T e s t p u b l i c v o i d t e s t A n n o t a t e d V a l u e ( ) t h r o w s E x c e p t i o n { f i n a l M e s s a g e m s g = n e w M e s s a g e ( ) ; m s g . s e t M s g ( " T e s t m e s s a g e " ) ; m s g . s e t C r e a t e d A t ( n e w D a t e ( ) ) ; f i n a l M o s h i m o s h i = n e w M o s h i . B u i l d e r ( ) . a d d ( D a t e . c l a s s , I S O 8 6 0 1 . c l a s s , n e w I S O 8 6 0 1 D a t e A d a p t e r ( ) ) . b u i l d ( ) ; f i n a l S t r i n g j s o n = m o s h i . a d a p t e r ( M e s s a g e . c l a s s ) . t o J s o n ( m s g ) ; } p u b l i c s t a t i c c l a s s M e s s a g e { S t r i n g m s g ; @ I S O 8 6 0 1 D a t e c r e a t e d A t ; } @ R e t e n t i o n ( R U N T I M E ) p u b l i c s t a t i c @ i n t e r f a c e I S O 8 6 0 1 { } -
How to use it Walk through the JSON structure @
T e s t p u b l i c v o i d t e s t W a l k T h r o u g h ( ) t h r o w s E x c e p t i o n { f i n a l S t r i n g j s o n = " { \ " u s e r s \ " : [ { \ " n a m e \ " : \ " J a k e W h a r t o n \ " , \ " a g e \ " : 3 1 } ] } " f i n a l J s o n R e a d e r r e a d e r = n e w J s o n R e a d e r ( n e w B u f f e r ( j s o n ) ) ; r e a d e r . b e g i n O b j e c t ( ) ; w h i l e ( r e a d e r . h a s N e x t ( ) ) { i f ( r e a d e r . n e x t N a m e ( ) . e q u a l s ( " u s e r s " ) ) { r e a d e r . b e g i n A r r a y ( ) ; w h i l e ( r e a d e r . h a s N e x t ( ) ) { r e a d e r . b e g i n O b j e c t ( ) ; w h i l e ( r e a d e r . h a s N e x t ( ) ) { s w i t c h ( r e a d e r . n e x t N a m e ( ) ) { / / s o m e o p e r a t i o n s } } r e a d e r . e n d O b j e c t ( ) ; } r e a d e r . e n d A r r a y ( ) ; } } r e a d e r . e n d O b j e c t ( ) ; } -
How to use it Inspect the current path @ T
e s t p u b l i c v o i d t e s t W a l k T h r o u g h ( ) t h r o w s E x c e p t i o n { f i n a l S t r i n g j s o n = " { \ " u s e r s \ " : [ { \ " n a m e \ " : \ " J a k e W h a r t o n \ " , \ " a g e \ " : 3 1 } ] } " f i n a l J s o n R e a d e r r e a d e r = n e w J s o n R e a d e r ( n e w B u f f e r ( j s o n ) ) ; r e a d e r . g e t P a t h ( ) ; / / = > " $ " r e a d e r . b e g i n O b j e c t ( ) ; r e a d e r . g e t P a t h ( ) ; / / = > " $ . " w h i l e ( r e a d e r . h a s N e x t ( ) ) { i f ( r e a d e r . n e x t N a m e ( ) . e q u a l s ( " u s e r s " ) ) { r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s " r e a d e r . b e g i n A r r a y ( ) ; r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s [ 0 ] " w h i l e ( r e a d e r . h a s N e x t ( ) ) { r e a d e r . b e g i n O b j e c t ( ) ; w h i l e ( r e a d e r . h a s N e x t ( ) ) { s w i t c h ( r e a d e r . n e x t N a m e ( ) ) { c a s e " n a m e " : r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s [ 0 ] . n a m e " r e a d e r . s k i p V a l u e ( ) ; r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s [ 0 ] . n u l l " b r e a k ; c a s e " a g e " : r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s [ 0 ] . a g e " r e a d e r . n e x t I n t ( ) ; r e a d e r . g e t P a t h ( ) ; / / = > " $ . u s e r s [ 0 ] . a g e " b r e a k ; } } -
Lenient Mode By default, this parser is strict and only
accepts JSON as specified by . RFC 4627 Streams that start with the , " ) ] } ' \ n " . Streams that include multiple top-level values. Top-level values of any type. Numbers may be NaNs or infinities. End of line comments starting with / / or # and ending with a newline character. C-style comments starting with / * and ending with * / . Such comments may not be nested. Names that are unquoted or ' s i n g l e q u o t e d ' . Strings that are unquoted or ' s i n g l e q u o t e d ' . Array elements separated by ; instead of , . Unnecessary array separators. These are interpreted as if null was the omitted value. Names and values separated by = or = > instead of : . Name/value pairs separated by ; instead of , . non-execute prefix -
Try Lenient Mode f i n a l S t
r i n g j s o n = " { n a m e : \ " J a k e W h a r t o n \ " , a g e : \ " 3 1 \ " , / / H o n e s t l y , I a m n o t s u r e h o w o l + " f v a l : N a N } { h o g e : f u g a } " ; f i n a l J s o n R e a d e r r e a d e r = n e w J s o n R e a d e r ( n e w B u f f e r ( j s o n ) ) ; r e a d e r . s e t L e n i e n t ( t r u e ) ; r e a d e r . b e g i n O b j e c t ( ) ; w h i l e ( r e a d e r . h a s N e x t ( ) ) { s w i t c h ( r e a d e r . n e x t N a m e ( ) ) { c a s e " f v a l " : r e a d e r . n e x t D o u b l e ( ) ; / / = > D o u b l e . N a N b r e a k ; c a s e " a g e " : r e a d e r . n e x t I n t ( ) ; / / = > 3 1 b r e a k ; d e f a u l t : r e a d e r . s k i p V a l u e ( ) ; } } -
First Look Intuitive Small set of features Registering an annotation
with a JsonAdapter is awesome! Supporting non-strict JSON (setLenient, string ⇔ int conversion) is nice. No advanced features yet (e.g. exclude policy, null handling, versioning support, etc.) A bit painful for registering JsonAdapter for a type everytime (cf. ) JsonReader/Writer seems good. g e t P a t h ( ) would be helpful when throwing an error. Issue #29 -
Discussion Based on okio. How about the performance? How about
memory usage? Might become a default JSON serializer/deserializer in Retrofit or OkHttp?