README.md in strong_json-0.3.0 vs README.md in strong_json-0.4.0
- old
+ new
@@ -28,11 +28,11 @@
let :item, object(id: prohibited, name: string, count: numeric)
let :customer, object(name: string, address: string, phone: string, email: optional(string))
let :order, object(customer: customer, items: array(item))
end
-json = s.order.coerce(JSON.parse(input), symbolize_names: true)
+json = s.order.coerce(JSON.parse(input, symbolize_names: true))
s.order =~ JSON.parse(input, symbolize_names: true)
case JSON.parse(input2, symbolize_names: true)
when s.item
# input2 is an item
@@ -54,9 +54,29 @@
### object(f1: type1, f2: type2, ...)
* The value must be an object
* Fields, `f1`, `f2`, and ..., must be present and its values must be of `type1`, `type2`, ..., respectively
* Objects with other fields will be rejected
+
+#### Performance hint
+
+Object attributes test is done in order of the keys.
+
+```ruby
+slower_object = enum(
+ object(id: numeric, created_at: string, updated_at: string, type: literal("person"), name: string),
+ object(id: numeric, created_at: string, updated_at: string, type: literal("food"), object: any)
+)
+
+faster_object = enum(
+ object(type: literal("person"), id: numeric, created_at: string, updated_at: string, name: string),
+ object(type: literal("food"), id: numeric, created_at: string, updated_at: string, object: any)
+)
+```
+
+The two enums represents same object, but testing runs faster with `faster_object`.
+Objects in `faster_object` have `type` attribute as their first keys.
+Testing `type` is done first, and it soon determines if the object is `"person"` or `"food"`.
### array(type)
* The value must be an array
* All elements in the array must be value of given `type`