1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
use lang:asm;
/**
* Class that represents a JSON value.
*
* The value is essentially a union of the following types:
* - Null
* - Bools
* - Number (int/float)
* - String
* - Array
* - Map (string to value)
*
* Newly created instances are "null" by default. This value will happily be converted into any of
* the supported types based on how it is used. Also, the number type is a bit flexible to match
* usage in Storm. For example, numbers are kept as integers as long as possible, but will act as if
* they are floats/doubles. Once the value is initialized to some type, it will throw an exception
* if it is used as the incorrect type.
*
* Note: this class is explicitly designed to "go around" the null-safety of Storm, and instead
* relies on exceptions. This is to make it easier to navigate deep data structures in a concise
* manner.
*/
class JsonValue {
// Default constructor, initialized to "null", which always throws when accessed until it is
// initialized to some value.
init() {
init { type = Type:empty; }
}
// Initialize to a bool.
cast(Bool v) {
init {
type = Type:bool;
numberVal = if (v) { 1l; } else { 0l; };
}
}
// Initialize to an integer.
cast(Byte v) { self(v.long); }
cast(Int v) { self(v.long); }
cast(Nat v) { self(v.long); }
cast(Word v) { self(v.long); }
cast(Long v) {
init {
type = Type:long;
numberVal = v;
}
}
// Initialize to a floating point number.
cast(Float v) { self(v.double); }
cast(Double v) {
Long converted;
asm { mov converted, v; }
init {
type = Type:double;
numberVal = converted;
}
}
// Initialize to a string.
cast(Str v) {
init {
type = Type:string;
objectVal = v;
}
}
// Initialize to an array.
init(JsonValue[] v) {
init {
type = Type:array;
objectVal = v;
}
}
// Initialize to a map (object).
init(Str->JsonValue v) {
init {
type = Type:object;
objectVal = v;
}
}
// Create initialized to an empty array.
JsonValue emptyArray() : static {
JsonValue(JsonValue[]);
}
// Create initialized to an empty object.
JsonValue emptyObject() : static {
JsonValue(Str->JsonValue);
}
// Compare to another value.
Bool ==(JsonValue other) {
if (type != other.type)
return false;
if (type == Type:bool) {
return compare(numberVal != 0, other.numberVal != 0);
} else if (type == Type:long) {
return compare(numberVal, other.numberVal);
} else if (type == Type:double) {
// TODO: Maybe with some epsilon?
return compare(numberVal, other.numberVal);
} else if (type == Type:empty) {
return true;
} else if (objectVal as Str) {
if (o = other.objectVal as Str)
return compare(objectVal, o);
else
return false;
} else if (objectVal as JsonValue[]) {
if (o = other.objectVal as JsonValue[])
return compare(objectVal, o);
else
return false;
} else if (objectVal as Str->JsonValue) {
if (o = other.objectVal as Str->JsonValue)
return compare(objectVal, o);
else
return false;
} else {
// Inconsistent representation.
return false;
}
}
// Does this class contain a non-null value?
Bool any() { type != Type:empty; }
Bool empty() { type == Type:empty; }
// Check type manually.
Bool isNull() { type == Type:empty; }
Bool isBool() { type == Type:bool; }
Bool isNumber() { (type == Type:long) | (type == Type:double); }
Bool isInteger() { type == Type:double; }
Bool isStr() { type == Type:string; }
Bool isArray() { type == Type:array; }
Bool isObject() { type == Type:object; }
// Type conversions. Throws if the type is incorrect.
Bool bool() {
if (type != Type:bool)
throw JsonAccessError("Expected bool", this);
numberVal != 0;
}
Byte byte() { long.byte; }
Int int() { long.int; }
Nat nat() { long.nat; }
Word word() { long.word; }
Long long() {
if (type != Type:long)
throw JsonAccessError("Expected integral number", this);
numberVal;
}
Float float() { double.float; }
Double double() {
Double result;
if (type == Type:long) {
result = numberVal.double;
} else if (type == Type:double) {
Long copy = numberVal;
asm { mov result, copy; }
} else {
throw JsonAccessError("Expected number", this);
}
result;
}
Str str() {
unless (objectVal as Str)
throw JsonAccessError("Expected string", this);
objectVal;
}
JsonValue[] array() {
unless (objectVal as JsonValue[])
throw JsonAccessError("Expected array", this);
objectVal;
}
Str->JsonValue object() {
unless (objectVal as Str->JsonValue)
throw JsonAccessError("Expected object", this);
objectVal;
}
// Array/object interface.
Nat count() {
if (objectVal as JsonValue[])
objectVal.count;
else if (objectVal as Str->JsonValue)
objectVal.count;
else
throw JsonAccessError("Expected array or object", this);
}
// Array interface.
JsonValue [](Nat id) {
array[id];
}
assign [](Nat id, JsonValue v) {
// TODO: Maybe insert values and convert to array?
array[id] = v;
}
JsonValue <<(JsonValue v) { push(v); this; }
void push(JsonValue v) {
if (type == Type:empty) {
objectVal = JsonValue[];
type = Type:array;
}
array.push(v);
}
void remove(Nat id) {
array.remove(id);
}
// Object interface.
JsonValue [](Str key) {
object.get(key);
}
assign [](Str key, JsonValue v) { put(key, v); }
JsonValue? at(Str key) {
object.at(key);
}
void put(Str key, JsonValue v) {
if (type == Type:empty) {
objectVal = Str->JsonValue;
type = Type:object;
}
object.put(key, v);
}
void remove(Str key) {
object.remove(key);
}
// Todo: Custom iterator that gives keys/values in relevant format?
// To string, providing format information. Pass `indent = 0` to skip linebreaks altogether.
Str toS(Nat indent) {
toS(indent, false);
}
// To string, provide the option to sort keys in objects.
Str toS(Nat indent, Bool sortKeys) {
StrBuf to;
if (indent > 0)
to.indentBy(" " * indent);
this.toS(to, indent == 0, sortKeys);
to.toS;
}
// To string, use the indentation already in strbuf.
void toS(StrBuf to) {
toS(to, false, false);
}
// To string, produce compact representation if asked to.
void toS(StrBuf to, Bool compact, Bool sortKeys) {
if (type == Type:bool) {
if (numberVal != 0)
to << "true";
else
to << "false";
} else if (type == Type:long) {
to << numberVal;
} else if (type == Type:double) {
Long copy = numberVal;
Double out;
asm { mov out, copy; }
// TODO: Proper formatting of NAN and denormalized values.
to << out;
} else if (objectVal as Str) {
formatStr(to, objectVal);
} else if (objectVal as JsonValue[]) {
to << "[";
for (i, v in objectVal) {
if (i > 0) {
if (compact)
to << ",";
else
to << ", ";
}
v.toS(to, compact, sortKeys);
}
to << "]";
} else if (objectVal as Str->JsonValue) {
to << "{";
if (!compact) {
to.indent();
}
if (sortKeys) {
putOrdered(to, objectVal, compact);
} else {
putUnordered(to, objectVal, compact);
}
if (!compact) {
to.dedent();
to << "\n";
}
to << "}";
} else {
to << "null";
}
}
// Helper to output unsorted keys.
private void putOrdered(StrBuf to, Str->JsonValue obj, Bool compact) : static {
Str[] order;
order.reserve(obj.count);
for (k, v in obj)
order << k;
order.sort();
Bool first = true;
for (k in order) {
if (first) {
if (!compact)
to << "\n";
} else {
if (compact)
to << ",";
else
to << ",\n";
}
first = false;
formatStr(to, k);
if (compact)
to << ":";
else
to << ": ";
obj.get(k).toS(to, compact, true);
}
}
// Helper to output unsorted keys.
private void putUnordered(StrBuf to, Str->JsonValue obj, Bool compact) : static {
Bool first = true;
for (k, v in obj) {
if (first) {
if (!compact)
to << "\n";
} else {
if (compact)
to << ",";
else
to << ",\n";
}
first = false;
formatStr(to, k);
if (compact)
to << ":";
else
to << ": ";
v.toS(to, compact, false);
}
}
// The actual data:
// Data type:
private enum Type {
empty,
// Numeric types are stored in the 'numberVal' variable. Even floating point types.
bool,
long,
double,
// Stored in 'objectVal' variable.
string,
array,
object,
}
private Type type;
private Long numberVal;
private Object? objectVal;
}
// Separate function to avoid recursing in the JsonValue.== function when comparing strings and integers.
private Bool compare(Long a, Long b) : inline {
a == b;
}
private Bool compare(Bool a, Bool b) : inline {
a == b;
}
private Bool compare(Str a, Str b) : inline {
a == b;
}
// Helper for comparison.
private Bool compare(JsonValue[] a, JsonValue[] b) {
if (a.count != b.count)
return false;
for (i, x in a)
if (!(x == b[i]))
return false;
return true;
}
private Bool compare(Str->JsonValue a, Str->JsonValue b) {
if (a.count != b.count)
return false;
for (k, x in a) {
unless (y = b.at(k))
return false;
if (!(x == y))
return false;
}
return true;
}
// Helper to format strings properly.
private void formatStr(StrBuf to, Str str) {
to << '"';
for (ch in str) {
if (ch == '\\') {
to << "\\\\";
} else if (ch == '"') {
to << "\\\"";
} else if (ch == '\n') {
to << "\\n";
} else if (ch == '\r') {
to << "\\r";
} else if (ch.codepoint <= 0x1F) {
// Need to be escaped, control character.
to << "\\u" << hex(ch.codepoint, 4);
} else if (ch.codepoint > 0x7F) {
// Outside of ascii, so escape for good measure. Note that JS works in UTF-16.
Nat leading = ch.utf16Leading();
Nat trailing = ch.utf16Trailing();
if (leading != 0)
to << "\\u" << hex(leading, 4);
to << "\\u" << hex(trailing, 4);
} else {
to << ch;
}
}
to << '"';
}
|