ClientLibrary Localization

Hi,
i have a problem if i add localizable items to a schema.
If i load the data via Postman with X-Languages and X-Flatten Header everything works as expected, but if i load the data via the ClientLibrary i got an exception:

An unhandled exception occurred while processing the request.
JsonSerializationException: Property must have a invariant language property.
Squidex.ClientLibrary.InvariantConverter.ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

My Code:

public async Task<List<Page>> GetPagesAsync()
        {
            var context = QueryContext.Default.WithLanguages("en").Flatten();
            var pages = await this.pagesClient.GetAsync(context: context);
            return pages.Items;
        }

Thank you,
Patrick

1 Like

Hi,

at the moment the InvariantConverter always requires the following structure for each property:

{ 
   "iv": "Foo"
}

You can just remove the converter from your properties.

1 Like

Hi,
it works now, thanks!

I have the same problem. I become the Error: JsonSerializationException: Property must have a invariant language property.
My Structur is:.
“data”: {

* "header":{
  * "en":[],

  * "de-DE":[
    * {
      * "Name": "string",

      * "Text": "stringstringstringst",

      * "Image":[],

      * "Slug": "stringstri",

      * "Tags":[]}],
} ....

My cs - File:

 public sealed class  PostData 
    {
       [JsonConverter(typeof(InvariantConverter))]
        public Header[] Header { get; set; }
        

        [JsonConverter(typeof(InvariantConverter))]
        public string Text { get; set; }
    }
    public sealed class Header
    {
        [JsonConverter(typeof(InvariantConverter))]
        public string Name{ get; set; }
        [JsonConverter(typeof(InvariantConverter))]
        public string Text { get; set; }
       ...
    }

MY API:

      var query = new ODataQuery { Skip = page * pageSize, Top = pageSize };
      var context = QueryContext.Default.WithLanguages("de").Flatten();

`var posts = await postsClient.GetAsync(query, context);`

How should the class structure look like?

Header needs to be a Dictionary<Header[]> Header { get; set; }

Sorry, I meant Dictionary<string, header[]> … the key is the language the value is the field value.

JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘System.Collections.Generic.Dictionary`2[System.String,Models.Header[]]’ because the type requires a JSON object (e.g. {“name”:“value”}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {“name”:“value”}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array

Unfortunately, that’s still not possible, I had already tried that. Is there possibly still another error? I have created an array of languages in the schema

Dictionary<string, Header> Header Does not work either

i can only write 3 replays. to the last comment: InvariantConverter
hi, I had meanwhile removed that. if I have it in the message comes:

JsonSerializationException: Property must have a invariant language property.

Sorry, you have to remove the InvariantConverter then.

I have removed the limitation. Can you show me your data class again?

Hi my Code . I use your Blogsample with changes:

 public sealed class PostsVM
        {
            public List<Post> Posts { get; set; }

            public long Total { get; set; }

            public long Page { get; set; }

            public long PageSize { get; set; }
        }


public sealed class Post : SquidexEntityBase<PostData>
    {
    }

   public sealed class  PostData 
    {
        public   Dictionary<string, Header> Header  { get; set; }

        [JsonConverter(typeof(InvariantConverter))]
        public string Text { get; set; }
    }

 public sealed class Header 
    {
        [JsonConverter(typeof(InvariantConverter))]
        public string Name{ get; set; }
        [JsonConverter(typeof(InvariantConverter))]
        public string Text { get; set; }
        [JsonConverter(typeof(InvariantConverter))]
        public string Previewtext { get; set; }
    }

Abfrage:

 public async Task<(long Total, List<Post> Posts)> GetPostsAsync(int page = 0, int pageSize = 3)
        {
            var query = new ODataQuery { Skip = page * pageSize, Top = pageSize };
            var context = QueryContext.Default.WithLanguages("de").Flatten();

            var posts = await postsClient.GetAsync(query, context);

            return (posts.Total, posts.Items);
        }

Hi,

in Squidex all root level fields are objects,

e.g.

for non localized fields

{
  "iv": "My-Value"
}

for localized fields

{
  "en": "My English Value",
  "de": "My English Value"
}

But invariant fields are annoying to handle in C#, therefore the InvariantConverter.

But they must not be applied to nested fields, because nested fields have the following structure:

{
  "iv": [{
    "nested": "My Nested Value"
  }]
}

,Now I remove all invariants in the Header. I still have the error. how would that be the solution?

It should be


public sealed class Post : SquidexEntityBase<PostData>
    {
    }

   public sealed class  PostData 
    {

        public   Dictionary<string, Header[]> Header  { get; set; }

        [JsonConverter(typeof(InvariantConverter))]
        public string Text { get; set; }
    }

 public sealed class Header 
    {
        public string Name{ get; set; }

        public string Text { get; set; }

        public string Previewtext { get; set; }
    }

Hi, thanks for the help. Unfortunately that did not work.
I also tried something.
With:
public Header[] Header {get; set; }

it works now.

1 Like