src: rename old `markdown` module in `docdown` to avoid merge conflicts
[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 # Static interface to get Nit objects from a Json string.
18 #
19 # `String::json_to_nit_object` returns an equivalent Nit object from
20 # the Json source. This object can then be type checked by the usual
21 # languages features (`isa` and `as`).
22 module static
23
24 import standard
25 private import json_parser
26 private import json_lexer
27
28 redef class Nvalue
29 fun to_nit_object: nullable Object is abstract
30 end
31
32 redef class Nvalue_number
33 redef fun to_nit_object
34 do
35 var text = n_number.text
36 if text.chars.has('.') or text.chars.has('e') or text.chars.has('E') then return text.to_f
37 return text.to_i
38 end
39 end
40
41 redef class Nvalue_string
42 redef fun to_nit_object do return n_string.to_nit_string
43 end
44
45 redef class Nvalue_true
46 redef fun to_nit_object do return true
47 end
48
49 redef class Nvalue_false
50 redef fun to_nit_object do return false
51 end
52
53 redef class Nvalue_null
54 redef fun to_nit_object do return null
55 end
56
57 redef class Nstring
58 # FIXME support \n, etc.
59 fun to_nit_string: String do return text.substring(1, text.length-2).
60 replace("\\\\", "\\").replace("\\\"", "\"").replace("\\b", "\b").
61 replace("\\/", "/").replace("\\n", "\n").replace("\\r", "\r").
62 replace("\\t", "\t")
63 end
64
65 redef class Nvalue_object
66 redef fun to_nit_object
67 do
68 var obj = new HashMap[String, nullable Object]
69 var members = n_members
70 if members != null then
71 var pairs = members.pairs
72 for pair in pairs do obj[pair.name] = pair.value
73 end
74 return obj
75 end
76 end
77
78 redef class Nmembers
79 fun pairs: Array[Npair] is abstract
80 end
81
82 redef class Nmembers_tail
83 redef fun pairs
84 do
85 var arr = n_members.pairs
86 arr.add n_pair
87 return arr
88 end
89 end
90
91 redef class Nmembers_head
92 redef fun pairs do return [n_pair]
93 end
94
95 redef class Npair
96 fun name: String do return n_string.to_nit_string
97 fun value: nullable Object do return n_value.to_nit_object
98 end
99
100 redef class Nvalue_array
101 redef fun to_nit_object
102 do
103 var arr = new Array[nullable Object]
104 var elements = n_elements
105 if elements != null then
106 var items = elements.items
107 for item in items do arr.add(item.to_nit_object)
108 end
109 return arr
110 end
111 end
112
113 redef class Nelements
114 fun items: Array[Nvalue] is abstract
115 end
116
117 redef class Nelements_tail
118 redef fun items
119 do
120 var items = n_elements.items
121 items.add(n_value)
122 return items
123 end
124 end
125
126 redef class Nelements_head
127 redef fun items do return [n_value]
128 end
129
130 redef class Text
131 fun json_to_nit_object: nullable Object
132 do
133 var lexer = new Lexer_json(to_s)
134 var parser = new Parser_json
135 var tokens = lexer.lex
136 parser.tokens.add_all(tokens)
137 var root_node = parser.parse
138 if root_node isa NStart then
139 return root_node.n_0.to_nit_object
140 else if root_node isa NLexerError then
141 var pos = root_node.position
142 print "Json lexer error: {root_node.message} at {pos or else "<unknown>"} for {root_node}"
143 return null
144 else if root_node isa NParserError then
145 var pos = root_node.position
146 print "Json parsing error: {root_node.message} at {pos or else "<unknown>"} for {root_node}"
147 return null
148 else abort
149 end
150 end