| | |
| | |
| |
|
| | |
| |
|
| | include "API.dfy" |
| | include "ZeroCopy/API.dfy" |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | module {:options "-functionSyntax:4"} JSON.Examples.AbstractSyntax { |
| | import API |
| | import opened Values |
| | import opened Wrappers |
| |
|
| | |
| | |
| | |
| |
|
| | import opened UnicodeStrings |
| |
|
| | |
| | |
| |
|
| | method {:test} Test() { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | var SIMPLE_JS :- expect ToUTF8Checked("[true]"); |
| | var SIMPLE_VALUE := Array([Bool(true)]); |
| | expect API.Deserialize(SIMPLE_JS) == Success(SIMPLE_VALUE); |
| |
|
| | |
| | |
| |
|
| | var CITIES_JS :- expect ToUTF8Checked(@"{ |
| | ""Cities"": [ |
| | { |
| | ""Name"": ""Boston"", |
| | ""Founded"": 1630, |
| | ""Population"": 689386, |
| | ""Area (km2)"": 4584.2 |
| | }, { |
| | ""Name"": ""Rome"", |
| | ""Founded"": -753, |
| | ""Population"": 2.873e6, |
| | ""Area (km2)"": 1285 |
| | }, { |
| | ""Name"": ""Paris"", |
| | ""Founded"": null, |
| | ""Population"": 2.161e6, |
| | ""Area (km2)"": 2383.5 |
| | } |
| | ] |
| | }"); |
| | var CITIES_VALUE := |
| | Object([ |
| | ("Cities", Array([ |
| | Object([ |
| | ("Name", String("Boston")), |
| | ("Founded", Number(Int(1630))), |
| | ("Population", Number(Int(689386))), |
| | ("Area (km2)", Number(Decimal(45842, -1))) |
| | ]), |
| | Object([ |
| | ("Name", String("Rome")), |
| | ("Founded", Number(Int(-753))), |
| | ("Population", Number(Decimal(2873, 3))), |
| | ("Area (km2)", Number(Int(1285))) |
| | ]), |
| | Object([ |
| | ("Name", String("Paris")), |
| | ("Founded", Null), |
| | ("Population", Number(Decimal(2161, 3))), |
| | ("Area (km2)", Number(Decimal(23835, -1))) |
| | ]) |
| | ])) |
| | ]); |
| | expect API.Deserialize(CITIES_JS) == Success(CITIES_VALUE); |
| |
|
| | |
| | |
| |
|
| | expect API.Serialize(SIMPLE_VALUE) == Success(SIMPLE_JS); |
| |
|
| | |
| |
|
| | var EXPECTED :- expect ToUTF8Checked( |
| | @"{""Cities"":[{""Name"":""Boston"",""Founded"":1630,""Population"":689386,""Area (km2)"":45842e-1},{""Name"":""Rome"",""Founded"":-753,""Population"":2873e3,""Area (km2)"":1285},{""Name"":""Paris"",""Founded"":null,""Population"":2161e3,""Area (km2)"":23835e-1}]}" |
| | ); |
| |
|
| | expect API.Serialize(CITIES_VALUE) == Success(EXPECTED); |
| |
|
| | |
| | |
| | |
| |
|
| | var CITY_JS :- expect ToUTF8Checked(@"{""Cities"": [{ |
| | ""Name"": ""Boston"", |
| | ""Founded"": 1630, |
| | ""Population"": 689386, |
| | ""Area (km2)"": 4584.2}]}"); |
| |
|
| | var CITY_VALUE := |
| | Object([("Cities", Array([ |
| | Object([ |
| | ("Name", String("Boston")), |
| | ("Founded", Number(Int(1630))), |
| | ("Population", Number(Int(689386))), |
| | ("Area (km2)", Number(Decimal(45842, -1)))])]))]); |
| |
|
| | expect API.Deserialize(CITY_JS) == Success(CITY_VALUE); |
| |
|
| | var EXPECTED' :- expect ToUTF8Checked( |
| | @"{""Cities"":[{""Name"":""Boston"",""Founded"":1630,""Population"":689386,""Area (km2)"":45842e-1}]}" |
| | ); |
| |
|
| | expect API.Serialize(CITY_VALUE) == Success(EXPECTED'); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | module {:options "-functionSyntax:4"} JSON.Examples.ConcreteSyntax { |
| | import ZeroCopy.API |
| | import opened UnicodeStrings |
| | import opened Grammar |
| | import opened Wrappers |
| |
|
| | |
| | |
| | |
| |
|
| | method {:test} Test() { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | var CITIES :- expect ToUTF8Checked(@"{ |
| | ""Cities"": [ |
| | { |
| | ""Name"": ""Boston"", |
| | ""Founded"": 1630, |
| | ""Population"": 689386, |
| | ""Area (km2)"": 4600 |
| | }, { |
| | ""Name"": ""Rome"", |
| | ""Founded"": -753, |
| | ""Population"": 2.873e6, |
| | ""Area (km2)"": 1285 |
| | }, { |
| | ""Name"": ""Paris"", |
| | ""Founded"": null, |
| | ""Population"": 2.161e6, |
| | ""Area (km2)"": 2383.5 |
| | } |
| | ] |
| | }"); |
| |
|
| | var deserialized :- expect API.Deserialize(CITIES); |
| | expect API.Serialize(deserialized) == Success(CITIES); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | var UNKNOWN_JS :- expect ToUTF8Checked(@"""Unknown"""); |
| | var UNKNOWN :- expect API.Deserialize(UNKNOWN_JS); |
| |
|
| | |
| | |
| | |
| |
|
| | var without_null := deserialized.(t := ReplaceNull(deserialized.t, UNKNOWN.t)); |
| |
|
| | |
| | |
| |
|
| | var expected_js :- expect ToUTF8Checked(@"{ |
| | ""Cities"": [ |
| | { |
| | ""Name"": ""Boston"", |
| | ""Founded"": 1630, |
| | ""Population"": 689386, |
| | ""Area (km2)"": 4600 |
| | }, { |
| | ""Name"": ""Rome"", |
| | ""Founded"": -753, |
| | ""Population"": 2.873e6, |
| | ""Area (km2)"": 1285 |
| | }, { |
| | ""Name"": ""Paris"", |
| | ""Founded"": ""Unknown"", |
| | ""Population"": 2.161e6, |
| | ""Area (km2)"": 2383.5 |
| | } |
| | ] |
| | }"); |
| | var actual_js :- expect API.Serialize(without_null); |
| | expect actual_js == expected_js; |
| | } |
| |
|
| | |
| |
|
| | import Seq |
| |
|
| | function ReplaceNull(js: Value, replacement: Value): Value { |
| | match js |
| |
|
| | |
| |
|
| | case Bool(_) => js |
| | case String(_) => js |
| | case Number(_) => js |
| |
|
| | |
| |
|
| | case Null(_) => replacement |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | case Object(obj) => |
| | Object(obj.(data := MapSuffixedSequence(obj.data, (s: Suffixed<jKeyValue, jcomma>) requires s in obj.data => |
| | s.t.(v := ReplaceNull(s.t.v, replacement))))) |
| | case Array(arr) => |
| | Array(arr.(data := MapSuffixedSequence(arr.data, (s: Suffixed<Value, jcomma>) requires s in arr.data => |
| | ReplaceNull(s.t, replacement)))) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | function MapSuffixedSequence<D, S>(sq: SuffixedSequence<D, S>, fn: Suffixed<D, S> --> D) |
| | : SuffixedSequence<D, S> |
| | requires forall suffixed | suffixed in sq :: fn.requires(suffixed) |
| | { |
| | |
| | |
| | var fn' := (sf: Suffixed<D, S>) requires (ghost var in_sq := sf => sf in sq; in_sq(sf)) => sf.(t := fn(sf)); |
| | var sq' := Seq.Map(fn', sq); |
| |
|
| | assert NoTrailingSuffix(sq') by { |
| | forall idx | 0 <= idx < |sq'| ensures sq'[idx].suffix.Empty? <==> idx == |sq'| - 1 { |
| | calc { |
| | sq'[idx].suffix.Empty?; |
| | fn'(sq[idx]).suffix.Empty?; |
| | sq[idx].suffix.Empty?; |
| | idx == |sq| - 1; |
| | idx == |sq'| - 1; |
| | } |
| | } |
| | } |
| |
|
| | sq' |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| |
|