Skip to content

Commit 4f25ca6

Browse files
Add doc for JSON->Plutus Data reversible embedding
1 parent b9d581e commit 4f25ca6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

coop-docs/05-json-plutus.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Reversible embedding from JSON to Plutus Data
2+
3+
Publishers would like to store and provide their Fact Statements in JSON format
4+
([ECMA-404](https://www.ecma-international.org/publications-and-standards/standards/ecma-404/)),
5+
but the Cardano Ledger ([Alonzo
6+
CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl))
7+
requires utxo datums to be serialized in the Plutus Data format
8+
([PlutusCore.Data](https://github.com/input-output-hk/plutus/blob/master/plutus-core/plutus-core/src/PlutusCore/Data.hs)).
9+
10+
In the COOP framework, we have adopted the following reversible embedding from
11+
JSON values to Plutus Data values. This embedding allows any JSON value to be
12+
converted into a Plutus Data value and allows the converted value to be
13+
converted back to the original JSON value. However, not all Plutus Data values
14+
can be converted to JSON by the embedding -- this is fine because the intended
15+
use for the embedding is to convert JSON to Plutus Data, publish the Plutus Data
16+
on the Cardano blockchain, read the Plutus Data from the Cardano blockchain, and
17+
convert it back to the original JSON.
18+
19+
A JSON value can be of any of the following:
20+
21+
- **Object** -- a collection of zero or more name-value pairs. Names must be
22+
strings, while values can be of any JSON type.
23+
- **Array** -- a sequence of zero or more values.
24+
- **Number** -- a floating-point number, excluding any numbers that cannot be
25+
represented using digits (e.g. Infinity and NaN).
26+
- **String** -- a sequence of UTF-8 code points.
27+
- **`true`**
28+
- **`false`**
29+
- **`null`**
30+
31+
A Plutus Data value can be any of the following:
32+
33+
- **Constructor** -- an integer-tagged sequence of values. This is intended to
34+
be used to represent sum types, which are types that provide multiple possible
35+
options for their values and use a different tag for each option. For example,
36+
the result of a fallible numeric calculation can be represented as either a
37+
textual description of an error or a numeric correct result of the
38+
calculation.
39+
- **Map** -- a collection of zero or more value-value pairs.
40+
- **List** -- a sequence of zero or more values.
41+
- **Integer** -- a whole number than can be zero (0), positive (1, 2, 3, ...),
42+
or negative (-1, -2, -3, ...).
43+
- **Bytestring** -- a sequence of bytes.
44+
45+
A JSON value can be converted to a Plutus Data value as follows:
46+
47+
- A JSON **Object** is converted into a Plutus Data **Map**. For each name-value
48+
pair in the JSON Object, the name (a JSON String) is converted into a Plutus
49+
Data Bytestring and the value is converted into a corresponding Plutus Data
50+
value.
51+
- A JSON **Array** is converted into a Plutus Data **List**. Each value in the
52+
JSON Array is converted into a corresponding Plutus Data value.
53+
- A JSON **Number** is converted into either a Plutus Data **Integer** or a
54+
Plutus Data **Constructor**:
55+
- If the JSON Number can be safely converted into an integer without rounding,
56+
then it is converted into a Plutus Data Integer.
57+
- Otherwise, the JSON Number is converted into a Plutus Data Constructor
58+
tagged by the integer `3`. The JSON Number's significand is placed as a
59+
Plutus Data Integer into the first position of the Constructor, and the JSON
60+
Number's base-10 exponent is place as a Plutus Data Integer into the second
61+
position.
62+
- A JSON **String** is converted into a Plutus Data **Bytestring** by encoding
63+
the sequence of UTF-8 code points into a sequence of bytes.
64+
- A JSON **`true`** value is converted into a Plutus Data **Constructor** tagged
65+
by the integer **`1`**, with an empty sequence of values.
66+
- A JSON **`false`** value is converted into a Plutus Data **Constructor**
67+
tagged by the integer **`0`**, with an empty sequence of values.
68+
- A JSON **`null`** value is converted into a Plutus Data **Constructor** tagged
69+
by the integer **`2`**, with an empty sequence of values.
70+
71+
A Plutus Data value that was derived via the embedding from a JSON value can
72+
always be converted back to that JSON value:
73+
74+
- A Plutus Data **Constructor** is converted into a corresponding JSON value
75+
based on its integer tag:
76+
- If the tag is **`0`**, then it is converted into a JSON **`false`** value.
77+
- If the tag is **`1`**, then it is converted into a JSON **`true`** value.
78+
- If the tag is **`2`**, then it is converted into a JSON **`null`** value.
79+
- If the tag is **`3`**, then a JSON **Number** is constructed using the
80+
significand in the Constructor's first position and the base-10 exponent in
81+
the Constructor's second position.
82+
- A Plutus Data **Map** is converted into a JSON **Object**. For each
83+
value-value pair in the Plutus Data Map, the first value is converted into a
84+
JSON String and the second value is converted into a corresponding JSON value.
85+
- A Plutus Data **List** is converted into a JSON **Array**. Each value in the
86+
Plutus Data List is converted into a corresponding JSON value.
87+
- A Plutus Data **Integer** is converted into a JSON **Number**, with the
88+
base-10 exponent set to 0.
89+
- A Plutus Data **Bytestring** is converted into a JSON **String** by decoding
90+
the sequence of bytes into a sequence of UTF-8 code points.
91+
92+
The conversion into JSON will fail for the following Plutus Data values:
93+
94+
- A Plutus Data **Constructor** tagged by a different integer than 0, 1, 2, or
95+
3.
96+
- A Plutus Data **Constructor** tagged by the integer 3 that does not contain
97+
exactly two Plutus Data Integers in its sequence of values.
98+
- A Plutus Data **Map** that contains a value-value pair whose first value
99+
cannot be converted into a JSON String.
100+
- A Plutus Data **Bytestring** that cannot be decoded into a sequence of UTF-8
101+
code points.
102+

0 commit comments

Comments
 (0)