1 package jreader 2 3 // AnyValue is returned by Reader.Any() to represent a JSON value of an arbitrary type. 4 type AnyValue struct { 5 // Kind describes the type of the JSON value. 6 Kind ValueKind 7 8 // Bool is the value if the JSON value is a boolean, or false otherwise. 9 Bool bool 10 11 // Number is the value if the JSON value is a number, or zero otherwise. 12 Number float64 13 14 // String is the value if the JSON value is a string, or an empty string otherwise. 15 String string 16 17 // Array is an ArrayState that can be used to iterate through the array elements if the JSON 18 // value is an array, or an uninitialized ArrayState{} otherwise. 19 Array ArrayState 20 21 // Object is an ObjectState that can be used to iterate through the object properties if the 22 // JSON value is an object, or an uninitialized ObjectState{} otherwise. 23 Object ObjectState 24 } 25 26 // ValueKind defines the allowable value types for Reader.Any. 27 type ValueKind int 28 29 const ( 30 // NullValue means the value is a null. 31 NullValue ValueKind = iota 32 33 // BoolValue means the value is a boolean. 34 BoolValue ValueKind = iota 35 36 // NumberValue means the value is a number. 37 NumberValue ValueKind = iota 38 39 // StringValue means the value is a string. 40 StringValue ValueKind = iota 41 42 // ArrayValue means the value is an array. 43 ArrayValue ValueKind = iota 44 45 // ObjectValue means the value is an object. 46 ObjectValue ValueKind = iota 47 ) 48 49 // String returns a description of the ValueKind. 50 func (k ValueKind) String() string { 51 switch k { 52 case NullValue: 53 return "null" 54 case BoolValue: 55 return "boolean" 56 case NumberValue: 57 return "number" 58 case StringValue: 59 return "string" 60 case ArrayValue: 61 return "array" 62 case ObjectValue: 63 return "object" 64 default: 65 return "unknown token" 66 } 67 } 68 69 // Readable is an interface for types that can read their data from a Reader. 70 type Readable interface { 71 // ReadFromJSONReader attempts to read the object's state from a Reader. 72 // 73 // This method does not need to return an error value because Reader remembers when it 74 // has encountered an error. 75 ReadFromJSONReader(*Reader) 76 } 77