Skip to content

Convert a JSON object to an Entities type #1066

@muse254

Description

@muse254

Category

User level API features/changes

Describe the feature you'd like to request

I have tried to look into the apis provided and can't find a way to convert a JSON dynamically to an Entity.
Say I have the JSON object

{
  "name": "John Doe",
  "age": 55,
  "is_staff": true,
  "contacts": {
    "email": "example@mail.com",
    "phone": 98978
  }
}

I would like to convert this to an Entities type with a function of maybe this signature

impl Entities {
   pub fn from_payload(schema: &Schema, type: &str, payload: &[u8])-> Result<Self>
}

Expected entities representation (might be erroneous)

[
    {
        "uid": {
            "type": "User",
            "id": "..."
        },
        "attrs": {
            "name": "John Doe",
            "age": 55,
            "is_staff": true,
            "contacts": {
                "type": "Contact",
                "id": "444"
            }
        },
        "parents": []
    },
    {
        "uid": {
            "type": "Contact",
            "id": "444"
        },
        "attrs": {
            "email": "example@mail.com",
            "phone": 98978
        },
        "parents": []
    }
]

It's not possible to write an own implementation since entityType is private to the crate consumer once parsed to the Schema type and also we wouldn't want to have different implementations in the wild.

Describe alternatives you've considered

I have tried to look into the apis provided for the Schema, Entities and can't find a way to convert a JSON dynamically to an Entities type.
Also, I haven't found any docs that describe this scenario.

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestThis issue requets a substantial new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions