lib: intro the friendlier to use json::dynamic
[nit.git] / lib / json / static.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module static
18
19 import json_parser
20 import json_lexer
21
22 redef class Nvalue
23 fun to_nit_object: nullable Object is abstract
24 end
25
26 redef class Nvalue_number
27 redef fun to_nit_object
28 do
29 var text = n_number.text
30 if text.chars.has('.') or text.chars.has('e') or text.chars.has('E') then return text.to_f
31 return text.to_i
32 end
33 end
34
35 redef class Nvalue_string
36 redef fun to_nit_object do return n_string.to_nit_string
37 end
38
39 redef class Nvalue_true
40 redef fun to_nit_object do return true
41 end
42
43 redef class Nvalue_false
44 redef fun to_nit_object do return false
45 end
46
47 redef class Nvalue_null
48 redef fun to_nit_object do return null
49 end
50
51 redef class Nstring
52 # FIXME support \n, etc.
53 fun to_nit_string: String do return text.substring(1, text.length-2).
54 replace("\\\\", "\\").replace("\\\"", "\"").replace("\\b", "\b").
55 replace("\\/", "/").replace("\\n", "\n").replace("\\r", "\r").
56 replace("\\t", "\t")
57 end
58
59 redef class Nvalue_object
60 redef fun to_nit_object
61 do
62 var obj = new HashMap[String, nullable Object]
63 var members = n_members
64 if members != null then
65 var pairs = members.pairs
66 for pair in pairs do obj[pair.name] = pair.value
67 end
68 return obj
69 end
70 end
71
72 redef class Nmembers
73 fun pairs: Array[Npair] is abstract
74 end
75
76 redef class Nmembers_tail
77 redef fun pairs
78 do
79 var arr = n_members.pairs
80 arr.add n_pair
81 return arr
82 end
83 end
84
85 redef class Nmembers_head
86 redef fun pairs do return [n_pair]
87 end
88
89 redef class Npair
90 fun name: String do return n_string.to_nit_string
91 fun value: nullable Object do return n_value.to_nit_object
92 end
93
94 redef class Nvalue_array
95 redef fun to_nit_object
96 do
97 var arr = new Array[nullable Object]
98 var elements = n_elements
99 if elements != null then
100 var items = elements.items
101 for item in items do arr.add(item.to_nit_object)
102 end
103 return arr
104 end
105 end
106
107 redef class Nelements
108 fun items: Array[Nvalue] is abstract
109 end
110
111 redef class Nelements_tail
112 redef fun items
113 do
114 var items = n_elements.items
115 items.add(n_value)
116 return items
117 end
118 end
119
120 redef class Nelements_head
121 redef fun items do return [n_value]
122 end
123
124 redef class String
125 fun json_to_nit_object: nullable Object
126 do
127 var lexer = new Lexer_json(self)
128 var parser = new Parser_json
129 var tokens = lexer.lex
130 parser.tokens.add_all(tokens)
131 var root_node = parser.parse
132 if root_node isa NStart then
133 return root_node.n_0.to_nit_object
134 else if root_node isa NLexerError then
135 var pos = root_node.position
136 print "Json lexer error: {root_node.message} at {pos or else "<unknown>"} for {root_node}"
137 return null
138 else if root_node isa NParserError then
139 var pos = root_node.position
140 print "Json parsing error: {root_node.message} at {pos or else "<unknown>"} for {root_node}"
141 return null
142 else abort
143 end
144 end