Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
137
Task/JSON/NetRexx/json-1.netrexx
Normal file
137
Task/JSON/NetRexx/json-1.netrexx
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.util.List
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONTokener
|
||||
import org.json.JSONException
|
||||
|
||||
/**
|
||||
* Using library from json.org
|
||||
*
|
||||
* @see http://www.json.org/java/index.html
|
||||
*/
|
||||
class RJson01 public
|
||||
|
||||
properties private constant
|
||||
JSON_DWARFS = '' -
|
||||
'{\n' -
|
||||
' "F1937_1" : {\n' -
|
||||
' "title" : "Snow White and the Seven Dwarfs",\n' -
|
||||
' "year" : 1937,\n' -
|
||||
' "medium" : "film",\n' -
|
||||
' "dwarfs" : [ "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc" ]\n' -
|
||||
' },\n' -
|
||||
' "F2012_1" : {\n' -
|
||||
' "title" : "Mirror, Mirror",\n' -
|
||||
' "year" : 2012,\n' -
|
||||
' "medium" : "film",\n' -
|
||||
' "dwarfs" : [ "Grimm", "Butcher", "Wolf", "Napoleon", "Half Pint", "Grub", "Chuckles" ]\n' -
|
||||
' },\n' -
|
||||
'}'
|
||||
|
||||
/**
|
||||
* A bean that looks like the following JSON
|
||||
* <pre>
|
||||
* {
|
||||
* "F2012_2" : {
|
||||
* "title" : "Snow White & the Huntsman",
|
||||
* "year" : 2012,
|
||||
* "medium" : "film",
|
||||
* "dwarfs" : [ "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion" ]
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
SAMPLE_BEAN = DwarfBean( -
|
||||
"F2012_2", -
|
||||
"Snow White & the Huntsman", -
|
||||
Long(2012), -
|
||||
"film", -
|
||||
Arrays.asList([String "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion"]) -
|
||||
)
|
||||
|
||||
method main(args = String[]) public static
|
||||
say json2bean(JSON_DWARFS)
|
||||
say
|
||||
say bean2json(SAMPLE_BEAN)
|
||||
say
|
||||
return
|
||||
|
||||
method json2bean(dwarfs) public static returns List
|
||||
say "Make beans from this JSON string:"
|
||||
say dwarfs
|
||||
jsonBeans = ArrayList()
|
||||
do
|
||||
jd = JSONObject(JSONTokener(dwarfs))
|
||||
ns = JSONObject.getNames(jd)
|
||||
name = String
|
||||
loop name over ns
|
||||
dwarves = ArrayList()
|
||||
jn = jd.getJSONObject(name)
|
||||
title = jn.getString('title')
|
||||
year = Long(jn.getLong('year'))
|
||||
medium = jn.getString('medium')
|
||||
dwa = jn.getJSONArray('dwarfs')
|
||||
loop di = 0 to dwa.length() - 1
|
||||
dwarves.add(dwa.getString(di))
|
||||
end di
|
||||
jb = DwarfBean(name, title, year, medium, dwarves)
|
||||
jsonBeans.add(jb.toString())
|
||||
end name
|
||||
catch ex = JSONException
|
||||
ex.printStackTrace()
|
||||
end
|
||||
return jsonBeans
|
||||
|
||||
method bean2json(sb = DwarfBean) public static returns String
|
||||
say "Make JSONObject from this bean:"
|
||||
say sb
|
||||
jsonString = String
|
||||
do
|
||||
jd = JSONObject(sb)
|
||||
jo = JSONObject()
|
||||
jo = jo.put(sb.keyGet(), jd)
|
||||
jsonString = jo.toString(2)
|
||||
catch ex = JSONException
|
||||
ex.printStackTrace()
|
||||
end
|
||||
return jsonString
|
||||
|
||||
-- =============================================================================
|
||||
class RJson01.DwarfBean public binary
|
||||
properties private
|
||||
key = String -- not part of bean
|
||||
properties indirect
|
||||
title = String
|
||||
year = Long
|
||||
medium = String
|
||||
dwarfs = List
|
||||
|
||||
method DwarfBean(key_ = String null, title_ = String null, year_ = Long null, medium_ = String null, dwarfs_ = List null) public
|
||||
keyPut(key_)
|
||||
setTitle(title_)
|
||||
setYear(year_)
|
||||
setMedium(medium_)
|
||||
setDwarfs(dwarfs_)
|
||||
return
|
||||
|
||||
method keyPut(key_ = String) public
|
||||
key = key_
|
||||
return
|
||||
|
||||
method keyGet() returns String
|
||||
return key
|
||||
|
||||
method toString public returns String
|
||||
ts = StringBuilder()
|
||||
ts.append(String.format("%s@%08x ", [Object this.getClass().getSimpleName(), Integer(hashCode())]))
|
||||
ts.append('[')
|
||||
ts.append('key='String.valueOf(keyGet())', ')
|
||||
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()
|
||||
81
Task/JSON/NetRexx/json-2.netrexx
Normal file
81
Task/JSON/NetRexx/json-2.netrexx
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/* 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue