http server
This commit is contained in:
44
std/json.zr
44
std/json.zr
@@ -1,11 +1,11 @@
|
||||
include "std/containers.zr"
|
||||
|
||||
const JSON_OBJECT = 0
|
||||
const JSON_ARRAY = 1
|
||||
const JSON_STRING = 2
|
||||
const JSON_NUMBER = 3
|
||||
const JSON_NULL = 4
|
||||
const JSON_BOOL = 5
|
||||
const json.OBJECT = 0
|
||||
const json.ARRAY = 1
|
||||
const json.STRING = 2
|
||||
const json.NUMBER = 3
|
||||
const json.NULL = 4
|
||||
const json.BOOL = 5
|
||||
|
||||
struct json.Value
|
||||
type: i64
|
||||
@@ -14,7 +14,7 @@ struct json.Value
|
||||
func json.Value.dump[v: json.Value] : str
|
||||
s := new str.Builder
|
||||
|
||||
if v->type == JSON_OBJECT
|
||||
if v->type == json.OBJECT
|
||||
map := v->value as HashMap
|
||||
keys := map->keys()
|
||||
s->append_char('{')
|
||||
@@ -29,7 +29,7 @@ func json.Value.dump[v: json.Value] : str
|
||||
s->append((value as json.Value)->dump())
|
||||
s->append_char('}')
|
||||
keys->free()
|
||||
else if v->type == JSON_ARRAY
|
||||
else if v->type == json.ARRAY
|
||||
arr := v->value as Array
|
||||
s->append_char('[')
|
||||
for i in 0..arr->size
|
||||
@@ -37,16 +37,16 @@ func json.Value.dump[v: json.Value] : str
|
||||
s->append_char(',')
|
||||
s->append((arr->nth(i) as json.Value)->dump())
|
||||
s->append_char(']')
|
||||
else if v->type == JSON_STRING
|
||||
else if v->type == json.STRING
|
||||
// TODO: escape sequences
|
||||
s->append_char('"')
|
||||
s->append(v->value as str)
|
||||
s->append_char('"')
|
||||
else if v->type == JSON_NUMBER
|
||||
else if v->type == json.NUMBER
|
||||
s->append((v->value as i64)->to_str())
|
||||
else if v->type == JSON_NULL
|
||||
else if v->type == json.NULL
|
||||
s->append("null")
|
||||
else if v->type == JSON_BOOL
|
||||
else if v->type == json.BOOL
|
||||
if v->value as bool
|
||||
s->append("true")
|
||||
else
|
||||
@@ -57,7 +57,7 @@ func json.Value.dump[v: json.Value] : str
|
||||
return s->build_and_free_buffer()
|
||||
|
||||
func json.Value.free[v: json.Value] : void
|
||||
if v->type == JSON_OBJECT
|
||||
if v->type == json.OBJECT
|
||||
map := v->value as HashMap
|
||||
keys := map->keys()
|
||||
for i in 0..keys->size
|
||||
@@ -66,12 +66,12 @@ func json.Value.free[v: json.Value] : void
|
||||
keys->free()
|
||||
|
||||
map->free_with_keys()
|
||||
else if v->type == JSON_ARRAY
|
||||
else if v->type == json.ARRAY
|
||||
arr := v->value as Array
|
||||
for i in 0..arr->size
|
||||
(arr->nth(i) as json.Value)->free()
|
||||
arr->free()
|
||||
else if v->type == JSON_STRING
|
||||
else if v->type == json.STRING
|
||||
(v->value as str)->free()
|
||||
|
||||
(v as ptr)->free()
|
||||
@@ -92,7 +92,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
json._skip_whitespace(s, i)
|
||||
|
||||
out->type = JSON_OBJECT
|
||||
out->type = json.OBJECT
|
||||
out->value = HashMap.new()
|
||||
|
||||
if s[mem.read64(i)] != '}'
|
||||
@@ -124,7 +124,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
json._skip_whitespace(s, i)
|
||||
|
||||
out->type = JSON_ARRAY
|
||||
out->type = json.ARRAY
|
||||
out->value = []
|
||||
|
||||
while s[mem.read64(i)] != ']'
|
||||
@@ -143,7 +143,7 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
panic("json.parse: expected ']'")
|
||||
mem.write64(i, mem.read64(i) + 1)
|
||||
else if s[mem.read64(i)] == '"'
|
||||
out->type = JSON_STRING
|
||||
out->type = json.STRING
|
||||
out->value = json._parse_string(s, i)
|
||||
else if s[mem.read64(i)]->is_digit() || s[mem.read64(i)] == '-'
|
||||
start := mem.read64(i)
|
||||
@@ -160,18 +160,18 @@ func json._parse_value[s: str, i: ptr] : json.Value
|
||||
len := mem.read64(i) - start
|
||||
|
||||
num_str := s->substr_n(start, len)
|
||||
out->type = JSON_NUMBER
|
||||
out->type = json.NUMBER
|
||||
out->value = num_str->parse_i64()
|
||||
num_str->free()
|
||||
else if s[mem.read64(i)] == 'n' && s[mem.read64(i)+1] == 'u' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 'l'
|
||||
out->type = JSON_NULL
|
||||
out->type = json.NULL
|
||||
mem.write64(i, mem.read64(i) + 4)
|
||||
else if s[mem.read64(i)] == 't' && s[mem.read64(i)+1] == 'r' && s[mem.read64(i)+2] == 'u' && s[mem.read64(i)+3] == 'e'
|
||||
out->type = JSON_BOOL
|
||||
out->type = json.BOOL
|
||||
out->value = true
|
||||
mem.write64(i, mem.read64(i) + 4)
|
||||
else if s[mem.read64(i)] == 'f' && s[mem.read64(i)+1] == 'a' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 's' && s[mem.read64(i)+4] == 'e'
|
||||
out->type = JSON_BOOL
|
||||
out->type = json.BOOL
|
||||
out->value = false
|
||||
mem.write64(i, mem.read64(i) + 5)
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user