Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧠 Logic: 🧱 json_prolog, add predicate to convert json to prolog terms #348

Closed
3 tasks done
bdeneux opened this issue Apr 26, 2023 · 3 comments · Fixed by #350
Closed
3 tasks done

🧠 Logic: 🧱 json_prolog, add predicate to convert json to prolog terms #348

bdeneux opened this issue Apr 26, 2023 · 3 comments · Fixed by #350
Assignees

Comments

@bdeneux
Copy link
Contributor

bdeneux commented Apr 26, 2023

📝 Purpose

In the logic module, we will need a set of new predicates to handle json conversion between json string to prolog term and vice versa.

🧪 Expected behavior

json_prolog(?Json, ?Term).

Where

  • Json is the string representation of the json
  • Term is an Atom that would be unified by the JSON representation as Prolog terms.

In addition, when passing Json and Term, this predicate return true if both result match.

The canonical representation for Term is:

Json Prolog Value
Object

Mapped to a term json(Attributes), where Attributes is a list of all JSON attributes mapped to a pair term -(Attribute, Value), where Attribute is an atom created from the JSON string and Value is the JSON value.

{
  "foo": "bar",
  "foobar": "barfoo"
}

Result:

json[-(foo, 'bar'), -(foobar, 'barfoo')]
Array List of JSON values

⚠️ An empty array is represented with a @ functor : @([]). See #398 for more details.

["foo", "bar"]

Result:

['foo', 'bar']
String Atom
"foo"

Result:

'foo'
Number Number
1

Result:

1

Constant true and false

Atom @(true) and @(false)

true

Result:

@true

Constant null

Atom @(null)

null

Result:

@null

Note: converting a simple string, boolean, integer or null value are valid json and will be unified to an simple atom.

To ensure determinism on the chain, the result of the JSON conversion is canonicalized by sorting the JSON name attribute. Same behavior for the json representation on prolog : {"b": "a", "a":"c"} => json([a-c,b-a]) and json([b-a,a-c]) => {"a":"c", "b":"a"}.

🎯 Example

Given the following JSON

{
  "s": "string",
  "n": 1,
  "a": ["string1", "string2"],
  "o": {
    "b": true,
    "n": null
  }
}
json([
  -(s, 'string'),
  -(n, 1),
  -(a, ['string1', 'string2']),
  -(o, json([
    -(b, @true),
    -(n, @null),
  ])
])

✅ Acceptance Criteria

  • The predicate is implemented according to the described behavior and examples
  • The predicate passes all test cases
  • Any related documentation is updated

🔗 References and linked predicate

@bdeneux bdeneux changed the title [DRAFT] 🧠 Logic: add predicate to convert json to prolog terms [DRAFT] 🧠 Logic: 🧱 json_prolog add predicate to convert json to prolog terms Apr 26, 2023
@bdeneux bdeneux changed the title [DRAFT] 🧠 Logic: 🧱 json_prolog add predicate to convert json to prolog terms [DRAFT] 🧠 Logic: 🧱 json_prolog, add predicate to convert json to prolog terms Apr 26, 2023
@amimart
Copy link
Member

amimart commented Apr 26, 2023

This is excellent! Thanks @bdeneux 🥇

I think this is functionally speaking complete and pretty suitable.

We may just need to mention the edge case of a simple string being a valid JSON.

@ccamel
Copy link
Member

ccamel commented Apr 26, 2023

That's brilliant @bdeneux 🤩!

@bdeneux bdeneux linked a pull request Apr 28, 2023 that will close this issue
@bdeneux bdeneux changed the title [DRAFT] 🧠 Logic: 🧱 json_prolog, add predicate to convert json to prolog terms 🧠 Logic: 🧱 json_prolog, add predicate to convert json to prolog terms Apr 28, 2023
@bdeneux
Copy link
Contributor Author

bdeneux commented Jul 7, 2023

Main specification updated since implementation of the json empty array fix : #398.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants