diff --git a/src/convert.nr b/src/convert.nr index f1b6053..7ca55c3 100644 --- a/src/convert.nr +++ b/src/convert.nr @@ -15,6 +15,9 @@ use crate::globals::CHAR_E; use crate::globals::CHAR_f; use crate::globals::CHAR_n; use crate::globals::CHAR_t; +use crate::globals::FIELD_true; +use crate::globals::FIELD_null; +use crate::globals::FIELD_false; use crate::JSON; use crate::Object; @@ -104,16 +107,35 @@ impl ByteArrayConversions for [u8; N] if (size == 0) { Option::none() } else { - let mut byte : u8 = self[0]; + let mut result : Option = Option::none(); - if (byte == QUOTATION_MARK) + if (self[0] == QUOTATION_MARK) { let json = self.parse(&mut 1, (size - 1), -1); - if (json.doc.len() != 0) { if (json.doc[0].value.len() != 0) { byte = json.doc[0].value[0]; } } + if (json.doc.len() != 0) + { + if (json.doc[0].value.len() != 0) + { + let byte : u8 = json.doc[0].value[0]; + let OK = ((byte == CHAR_t) | (byte == CHAR_n) | (byte == CHAR_f)); + if (OK) { result = Option::some(byte == CHAR_t); } + } + } + } + else + { + let mut literal_field = (self[0] as Field); + for i in 1..size + { + literal_field *= 256; + literal_field += (self[i] as Field); + } + + let OK = ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false)); + if (OK) { result = Option::some(literal_field == FIELD_true); } } - let OK = ((byte == CHAR_t) | (byte == CHAR_n) | (byte == CHAR_f)); - if (OK) { Option::some(byte == CHAR_t) } else { Option::none() } + result } } @@ -216,8 +238,8 @@ impl ByteArrayConversions for str { unconstrained pub fn as_bool(self) -> Option { self.as_bytes().as_bool() } unconstrained pub fn as_field(self) -> Option { self.as_bytes().as_field() } - unconstrained pub fn as_list(self) -> [[u8]] { self.as_bytes().as_list() } unconstrained pub fn as_string(self) -> [u8] { self.as_bytes().as_slice() } + unconstrained pub fn as_list(self) -> [[u8]] { self.as_bytes().as_list() } unconstrained pub fn as_object(self) -> JSON { self.as_bytes().as_object() } } @@ -236,7 +258,7 @@ impl JSON let size : Field = bytes.len(); if (size == 0) { JSON::none() } { - if (bytes[0] == QUOTATION_MARK) { bytes.as_object() } + if (bytes[0] == QUOTATION_MARK) { bytes.parse(&mut 1, (size - 1), -1) } else { let mut result : JSON = JSON::none(); diff --git a/src/globals.nr b/src/globals.nr index 46c2566..3b6c893 100755 --- a/src/globals.nr +++ b/src/globals.nr @@ -22,3 +22,7 @@ global CHAR_E : u8 = 0x45; global CHAR_f : u8 = 0x66; global CHAR_n : u8 = 0x6E; global CHAR_t : u8 = 0x74; + +global FIELD_true : Field = 0x74727565; +global FIELD_null : Field = 0x6E756C6C; +global FIELD_false : Field = 0x66616C7365; diff --git a/src/parse.nr b/src/parse.nr index 01f4fe4..ecc8e89 100644 --- a/src/parse.nr +++ b/src/parse.nr @@ -16,18 +16,14 @@ use crate::globals::ZERO; use crate::globals::POINT; use crate::globals::CHAR_e; use crate::globals::CHAR_E; +use crate::globals::FIELD_true; +use crate::globals::FIELD_null; +use crate::globals::FIELD_false; use crate::JSON; use crate::Object; use crate::Property; -global FIELD_t : Field = 0x74; -global FIELD_tr : Field = 0x7472; -global FIELD_tru : Field = 0x747275; -global FIELD_true : Field = 0x74727565; -global FIELD_null : Field = 0x6E756C6C; -global FIELD_false : Field = 0x66616C7365; - trait PropertyLookup { fn get (self, key : str) -> [u8];