0

I was sending to a customer a list of 1 millions FOO in json.

public class FOO
{
    public string Foo, Name, Call, Loc, ID;
    public double Long, Lat;
    public string[] Adr, OpenningHours;
}           

But the customer ask me for a specific output as it will reduce the data size:

Remove the Property name, join the address, and flattern that list their will always be 7 day in a week. Like:

[
    [
       "ag",
       99.0000000,
       9.0000000,
       "FOO-FOO BAR",
       "ADR1|ADR2|ADR3|ADR4",
       "0101",
       "07:30-12:00,12:00-19:30",
       "07:30-12:00,12:00-19:30",
       "07:30-12:00,12:00-19:30",
       "07:30-12:00,12:00-19:30",
       "07:30-12:00,12:00-19:30",
       "07:30-13:00,",
       ",",
       "07H30",
       "FOO BAR"
    ],
]

I have no issue with the with the Address join or the flatern OpenningHours. Using:

public class ItemMapper
{
    public string Foo, Name, Adr, ID, Call, Loc,
        w1, w2, w3, w4, w5, w6, w7;
    public double Long, Lat;
}

return  
    new ItemMapper
    {
        Foo = this.Foo,
        // [...]
        Adr = string.Join("|", this.Adr),
        w1 = this.OpenningHours[0],
        // [...]
        w7= this.OpenningHours[6]
    }; 

Is there a proper way to do that without pure string manipulation Replace etc?

The serialisation of a List give a bad result, that I fix with a compilled regex remplace :

[
    { <-- Not a []
       "PopertyName":"PropertyValue", <-- Poperty Name, and :
       // ..
       "PopertyName":"PropertyValue",
    },
]
xdtTransform
  • 1,986
  • 14
  • 34
  • Possible duplicate of [Json.Net: Serialize/Deserialize property as a value, not as an object](https://stackoverflow.com/questions/40480489/json-net-serialize-deserialize-property-as-a-value-not-as-an-object) – FIL Aug 31 '18 at 13:04
  • @Fil , Linked duplicate expected json do not match mine. And the answer does not apply directly. Yes a write your own jconverter could be a good way but it's to short to be an answer. It's not about dict. – xdtTransform Aug 31 '18 at 13:29
  • Sometimes the answer is simple, I guess I was missdirected by the make it weird requirement. – xdtTransform Aug 31 '18 at 13:57

1 Answers1

1

I wouldn't recommend it as it gives you a dynamic JSON (i.e you cannot make classes out of it), but you can use a List<List<string>> to solve that requirement.

Given List<ItemMapper> data, use:

var result = data
    .Select(item => new List<string>
    {
        x.Foo,
        x.Name,
        x.Call,
        ... // not sure about the order though
    })
    .ToList();
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120