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

Serializing List<Person> #14

Open
shay99999 opened this issue Mar 6, 2018 · 2 comments
Open

Serializing List<Person> #14

shay99999 opened this issue Mar 6, 2018 · 2 comments

Comments

@shay99999
Copy link

Seems to be an issue with serialization of lists?

List persons = new List();
persons.Add(new Person(....
persons.Add(new Person(....

IEntitySerializer serializer = new EntitySerializer();
dynamic json = serializer.Serialize(persons);

Results in the following error: Object Serialized to Array. JObject instance expected.

Funnily enough seems to serialize a dictionary of persons ok though.

@tpluscode
Copy link
Contributor

Oh, hi @shay99999. I don't know how it happened but I completely missed your report. Let me try to reproduce.

@tpluscode
Copy link
Contributor

Okay, so obviously I never consider such usage of the serialiser and it expects input which will serialise to JSON object (JObject.FromObject()). A dictionary is handled as a JSON object because it is essentially a JS map and not an array.

I wouldn't want to overthink this at this point. Essentially JSON-LD can serialise arrays in two ways. First is a plain array, which prevents the use of a global @context

[
  {
    "@id": "http://example.com/resource"
  }
]

The second option enforces wrapping the array in a @graph object:

{
  "@context": {
    "@base": "http://example.com/"
  },
  "@graph": [
    {
      "@id": "resource"
    }
  ]
}

I'm pretty sure that for simplicity's sake I'd want to always do the latter and extend the entity serializer with a method JObject Serialize<TElement>(IEnumerable<TElement> entities, SerializationOptions options = null); This method will wrap the array in a @graph and apply the context for the element's type.
Could also be an extension.

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