82 lines
2.4 KiB
Plaintext
82 lines
2.4 KiB
Plaintext
/* NetRexx */
|
|
options replace format comments java crossref symbols nobinary
|
|
|
|
import com.google.gson.
|
|
import java.util.List
|
|
|
|
/**
|
|
* Using google-gson library
|
|
*
|
|
* @see https://code.google.com/p/google-gson/
|
|
*/
|
|
class RJson02 public
|
|
|
|
properties private constant
|
|
JSON_DWARFS = '' -
|
|
'{\n' -
|
|
' "title" : "Snow White and the Seven Dwarfs",\n' -
|
|
' "year" : 1937,\n' -
|
|
' "medium": "film",\n' -
|
|
' "dwarfs": [ "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc" ]\n' -
|
|
'}'
|
|
|
|
/**
|
|
* A bean that looks like the following JSON
|
|
* <pre>
|
|
* {
|
|
* "title" : "Snow White & the Huntsman",
|
|
* "year" : 2012,
|
|
* "medium" : "film",
|
|
* "dwarfs" : [ "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion" ]
|
|
* }
|
|
* </pre>
|
|
*/
|
|
SAMPLE_BEAN = RJSON02.DwarfBean( -
|
|
/*"F2012_2",*/ -
|
|
"Snow White and the Huntsman", -
|
|
Long(2012), -
|
|
"film", -
|
|
Arrays.asList([String "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion"]) -
|
|
)
|
|
|
|
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
method main(args = String[]) public static
|
|
gsonObj = GsonBuilder().setPrettyPrinting().create()
|
|
jsonBean = RJson02.DwarfBean gsonObj.fromJson(JSON_DWARFS, RJson02.DwarfBean.class)
|
|
say JSON_DWARFS
|
|
say jsonBean.toString()
|
|
say
|
|
|
|
json = gsonObj.toJson(SAMPLE_BEAN);
|
|
say json
|
|
say SAMPLE_BEAN.toString()
|
|
say
|
|
|
|
return
|
|
|
|
-- =============================================================================
|
|
class RJson02.DwarfBean public binary
|
|
properties indirect
|
|
title = String
|
|
year = Long
|
|
medium = String
|
|
dwarfs = List
|
|
|
|
method DwarfBean(title_ = String null, year_ = Long null, medium_ = String null, dwarfs_ = List null) public
|
|
setTitle(title_)
|
|
setYear(year_)
|
|
setMedium(medium_)
|
|
setDwarfs(dwarfs_)
|
|
return
|
|
|
|
method toString public returns String
|
|
ts = StringBuilder()
|
|
ts.append(String.format("%s@%08x ", [Object this.getClass().getSimpleName(), Integer(hashCode())]))
|
|
ts.append('[')
|
|
ts.append('title='String.valueOf(getTitle())', ')
|
|
ts.append('year='String.valueOf(getYear())', ')
|
|
ts.append('medium='String.valueOf(getMedium())', ')
|
|
ts.append('dwarfs='String.valueOf(getDwarfs()))
|
|
ts.append(']')
|
|
return ts.toString()
|