From becb8a0f7250b306e263ed9c79054ad4e5414c6d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 15 Jun 2020 11:08:23 +0300 Subject: [PATCH] compiler: support `System.Json.*` syscalls --- pkg/compiler/syscall.go | 4 ++++ pkg/interop/json/json.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 pkg/interop/json/json.go diff --git a/pkg/compiler/syscall.go b/pkg/compiler/syscall.go index 0be51d22a..2a4b12fa9 100644 --- a/pkg/compiler/syscall.go +++ b/pkg/compiler/syscall.go @@ -10,6 +10,10 @@ var syscalls = map[string]map[string]string{ "Next": "System.Enumerator.Next", "Value": "System.Enumerator.Value", }, + "json": { + "Serialize": "System.Json.Serialize", + "Deserialize": "System.Json.Deserialize", + }, "storage": { "ConvertContextToReadOnly": "System.Storage.AsReadOnly", "Delete": "System.Storage.Delete", diff --git a/pkg/interop/json/json.go b/pkg/interop/json/json.go new file mode 100644 index 000000000..ac9183ca8 --- /dev/null +++ b/pkg/interop/json/json.go @@ -0,0 +1,28 @@ +/* +Package json provides various JSON serialization/deserialization routines. +*/ +package json + +// ToJSON serializes value to json. It uses `System.Json.Serialize` syscall. +// Serialization format is the following: +// []byte -> base64 string +// bool -> json boolean +// nil -> Null +// string -> base64 encoded sequence of underlying bytes +// (u)int* -> integer, only value in -2^53..2^53 are allowed +// []interface{} -> json array +// map[type1]type2 -> json object with string keys marshaled as strings (not base64). +func ToJSON(item interface{}) []byte { + return nil +} + +// FromJSON deserializes value from json. It uses `System.Json.Deserialize` syscall. +// It performs deserialization as follows: +// strings -> []byte (string) from base64 +// integers -> (u)int* types +// null -> interface{}(nil) +// arrays -> []interface{} +// maps -> map[string]interface{} +func FromJSON(data []byte) interface{} { + return nil +}