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

How to define the json object #335

Open
oovm opened this issue Apr 9, 2024 · 1 comment
Open

How to define the json object #335

oovm opened this issue Apr 9, 2024 · 1 comment

Comments

@oovm
Copy link

oovm commented Apr 9, 2024

I want to define a json structure to provide serialization and deserialization capabilities, but I encountered two problems.

  1. No mapping of dictionary type
  2. Unable to define recursive types (direct or indirect)

package endec: [email protected];

world imports {
    export types;
}

interface types {
    /// The json value context
    record json {
        /// The root value
        root: value
    }
    /// A json value
    variant value {
        %null,
        %bool(bool),
        %decimal(f64),
        %string(string),
        %array(array),
        %object(object)
    }
    /// A json array
    record array {
        %list: list<value>,
    }
    /// A json object
    record object {
        %dict: list<tuple<string, value>>,
    }
}

Such a definition will report an error:

called `Result::unwrap()` on an `Err` value: failed to parse package: C:\Users\CLionProjects\serde-wasi\projects\serde-json\wit

Caused by:
    type `value` depends on itself
         --> C:\Users\CLionProjects\serde-wasi\projects\serde-json\wit\world.wit:11:15
          |
       11 |         root: value
          |               ^----
@lukewagner
Copy link
Member

Currently value types are not allowed to be recursive, which is the root problem here. It's definitely valuable and tracked as a future feature in #56, it's just a ton of effort and complexity so we were planning to postpone it until after the 1.0/MVP. Until then, the best approximation is to define JSON via resource types (which admittedly isn't very efficient due to the function call overhead, so you might also just want to pass JSON as a string or BSON list<u8>).

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

No branches or pull requests

2 participants