Browse Source

Support NaN and Infinity for JSON5

gingerBill 6 years ago
parent
commit
08598b9425
2 changed files with 38 additions and 14 deletions
  1. 22 0
      core/encoding/json/parser.odin
  2. 16 14
      core/encoding/json/validator.odin

+ 22 - 0
core/encoding/json/parser.odin

@@ -93,6 +93,28 @@ parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) {
 
 	case Kind.Open_Bracket:
 		return parse_array(p);
+
+	case:
+		if p.spec == Specification.JSON5 {
+			switch token.kind {
+			case Kind.Infinity:
+				inf: u64 = 0x7ff0000000000000;
+				if token.text[0] == '-' {
+					inf = 0xfff0000000000000;
+				}
+				value.value = transmute(f64)inf;
+				advance_token(p);
+				return;
+			case Kind.NaN:
+				nan: u64 = 0x7ff7ffffffffffff;
+				if token.text[0] == '-' {
+					nan = 0xfff7ffffffffffff;
+				}
+				value.value = transmute(f64)nan;
+				advance_token(p);
+				return;
+			}
+		}
 	}
 
 	err = Error.Unexpected_Token;

+ 16 - 14
core/encoding/json/validator.odin

@@ -90,31 +90,33 @@ validate_array :: proc(p: ^Parser) -> bool {
 
 validate_value :: proc(p: ^Parser) -> bool {
 	token := p.curr_token;
+
+	using Kind;
 	switch token.kind {
-	case Kind.Null:
-		advance_token(p);
-		return true;
-	case Kind.False:
+	case Null, False, True:
 		advance_token(p);
 		return true;
-	case Kind.True:
+	case Integer, Float:
 		advance_token(p);
 		return true;
-	case Kind.Integer:
-		advance_token(p);
-		return true;
-	case Kind.Float:
-		advance_token(p);
-		return true;
-	case Kind.String:
+	case String:
 		advance_token(p);
 		return is_valid_string_literal(token.text, p.spec);
 
-	case Kind.Open_Brace:
+	case Open_Brace:
 		return validate_object(p);
 
-	case Kind.Open_Bracket:
+	case Open_Bracket:
 		return validate_array(p);
+
+	case:
+		if p.spec == Specification.JSON5 {
+			switch token.kind {
+			case Infinity, NaN:
+				advance_token(p);
+				return true;
+			}
+		}
 	}
 
 	return false;