This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
/
load_json.html
144 lines (126 loc) · 4.77 KB
/
load_json.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Documentation - jsonstream</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.15.0/css/uikit.min.css">
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.15.0/js/uikit.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
<style>
.nav-sub-1 {
margin-left: 20px;
}
.nav-sub-2 {
margin-left: 40px;
}
</style>
<script>
$(function () {
var page = location.pathname.substring(1);
if (page === '') {
page = 'index.html';
}
$('ul.uk-nav li a[href="' + page + '"]').parent().addClass('uk-active')
});
</script>
</head>
<body>
<div class="uk-container uk-container-center">
<div class="uk-grid">
<div class="uk-width-1-1">
<div style="height: 20px"></div>
<h1>
<img src="icon.png"> jsonstream
</h1>
<h3>fast JSON serialization / deserialization with JavaBean
binding and validation</h3>
<hr />
</div>
</div>
<div class="uk-grid">
<div class="uk-width-1-5">
<div class="uk-panel">
<ul class="uk-nav uk-nav-side uk-nav-parent-icon">
<li><a href="index.html">Overview</a></li>
<li><a href="installation.html">Installation</a></li>
<li class="uk-nav-divider"></li>
<li><a href="user_guide.html">User Guide</a></li>
<li class="nav-sub-1"><a href="load_json.html">Load JSON</a></li>
<li class="nav-sub-1"><a href="json_to_bean.html">JSON to Bean</a></li>
<li class="nav-sub-1"><a href="validation.html">Validation</a></li>
<li class="nav-sub-1"><a href="type_adapter.html">Type Adapter</a></li>
<li class="nav-sub-1"><a href="write_json.html">Write JSON</a></li>
<li class="nav-sub-1"><a href="custom_serialization.html">Custom Serialization</a></li>
<li class="uk-nav-divider"></li>
<li><a href="apidocs/index.html" target="_blank">API Docs</a></li>
<li><a href="source_code.html">Source Code</a></li>
<li><a href="common_faqs.html">Common FAQs</a></li>
</ul>
</div>
</div>
<div class="uk-width-4-5">
<!-- BEGIN CONTENT -->
<h3>Load JSON</h3>
<p>You can call <code>JsonReader.parse()</code> to parse a JSON string to Java object, and
the following rules are applied:</p>
<ul>
<li><p>JSON value <code>true</code> or <code>false</code> is parsed as Java Boolean object with <code>true</code> or <code>false</code>.</p></li>
<li><p>JSON value <code>"string"</code> is parsed as Java String.</p></li>
<li><p>JSON value <code>null</code> is parsed as Java <code>null</code>.</p></li>
<li><p>JSON value <code>12345</code> is parsed as Java Long with a long value.</p></li>
<li><p>JSON value <code>123.45</code> or <code>1.2e3.4</code> is parsed as Java Double with a double value.</p></li>
<li><p>JSON object <code>{"key": "value"}</code> is parsed as Java Map with generic type <code>Map<String, Object></code>.</p></li>
<li><p>JSON array <code>[1, 2, 3]</code> is parsed as Java List with generic type <code>List<Object></code>.</p></li>
</ul>
<p>You can cast the returned object if you know the extract type of JSON string:</p>
<pre><code>Boolean b = (Boolean) reader.parse();
</code></pre>
<p>If you do not want to use the type-cast operation, you can use the generic method:</p>
<pre><code>Boolean b = reader.parse(Boolean.class);
</code></pre>
<p>It is very useful to get an integer because jsonstream parses JSON number as Long or Double:</p>
<pre><code>// wanted an integer number:
Long n = reader1.parse(Long.class);
// wanted a float number:
Double d = reader2.parse(Double.class);
// don't care the type of number:
Number x = reader3.parse(Number.class);
// got a Long or Double type, both are safe to convert to Number object.
</code></pre>
<h3>Using JsonObjectFactory and JsonArrayFactory</h3>
<p>JsonReader creates <code>HashMap<String, Object></code> and <code>ArrayList<Object></code> when parse JSON object and JSON array.
But you can specify your own <code>JsonObjectFactory</code> and <code>JsonArrayFactory</code> to return different underlying data type:</p>
<pre><code>JsonBuilder jb = new JsonBuilder().useJsonObjectFactory(
() -> {
return new TreeMap<String, Object>();
}
).useJsonArrayFactory(
() -> {
return new LinkedList<Object>();
}
);
</code></pre>
<!-- END CONTENT -->
</div>
</div>
<!-- footer -->
<div class="grid">
<div class="uk-width-1-1">
<hr />
<div class="uk-text-center">
<p>
<a href="https://github.com/michaelliao">Michael Liao</a> -
Copyright©2015.
</p>
</div>
</div>
</div>
</div>
</body>
</html>