Skip to content

[bug] Can not deserialize array at position 0 to list: It has a non-integer key #40

@sommmen

Description

@sommmen

Hiya just checking v2 out - seems to run into some issues.

Input

a:2:{i:0;a:5:{s:4:"name";s:4:"UPS5";s:4:"slug";s:7:"ups5-1C";s:4:"type";s:4:"ups5";s:5:"price";d:2.5;s:5:"dates";a:2:{s:4:"from";s:10:"1730790000";s:2:"to";s:10:"1730826000";}}i:1;a:5:{s:4:"name";s:8:"DPD3-B2C";s:4:"slug";s:11:"dpd3-b2c-1C";s:4:"type";s:8:"dpd3-b2c";s:5:"price";d:2.5;s:5:"dates";a:2:{s:4:"from";s:10:"1730790000";s:2:"to";s:10:"1730826000";}}}

Model

public class MyPhpModel
{
    [PhpProperty("name")] public string? Name { get; set; }
    [PhpProperty("slug")] public string? Slug { get; set; }
    [PhpProperty("type")] public string? Type { get; set; }
    [PhpProperty("price")] public decimal? Price { get; set; }
    [PhpProperty("dates")] public MyDatesPhpModel? Dates { get; set; }
}

public class MyDatesPhpModel
{
    [PhpProperty("from")]
    public string? From { get; set; }

    [PhpProperty("to")]
    public string? To { get; set; }
}

Code

    private static readonly PhpDeserializationOptions PhpDeserializationOptions = new()
    {
        AllowExcessKeys = true
    };  
    ...
    // PropertyType is List<MyPhpModel>
PhpSerialization.Deserialize(value, propertyType, PhpDeserializationOptions);

Exception

PhpSerializerNET.DeserializationException
  HResult=0x80131500
  Message=Can not deserialize array at position 0 to list: It has a non-integer key 'name' at element 2 (position 14).
  Source=PhpSerializerNET
  StackTrace:
   at PhpSerializerNET.PhpDeserializer.MakeList(Type targetType, PhpToken& token)
   at PhpSerializerNET.PhpDeserializer.DeserializeToken(Type targetType)
   at PhpSerializerNET.PhpDeserializer.Deserialize(Type targetType)
   at PhpSerializerNET.PhpSerialization.Deserialize(String input, Type type, PhpDeserializationOptions options)
   at OPG.WordPressService.Parsing.Attributes.Converters.PhpStringConverter.Convert(String value, Type propertyType, IFormatProvider formatProvider) in C:\Repos\dotnet-api\WordPressService\Parsing\Attributes\Converters\PhpStringConverter.cs:line 24

https://www.unserialize.com/s/312e060c-0e1c-db28-c39b-000037672b1b

array(2) {
  [0]=>
  array(5) {
    ["name"]=>
    string(4) "UPS5"
    ["slug"]=>
    string(7) "ups5-1C"
    ["type"]=>
    string(4) "ups5"
    ["price"]=>
    float(2.5)
    ["dates"]=>
    array(2) {
      ["from"]=>
      string(10) "1730790000"
      ["to"]=>
      string(10) "1730826000"
    }
  }
  [1]=>
  array(5) {
    ["name"]=>
    string(8) "DPD3-B2C"
    ["slug"]=>
    string(11) "dpd3-b2c-1C"
    ["type"]=>
    string(8) "dpd3-b2c"
    ["price"]=>
    float(2.5)
    ["dates"]=>
    array(2) {
      ["from"]=>
      string(10) "1730790000"
      ["to"]=>
      string(10) "1730826000"
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions