Jamie
Jamie

Reputation: 10906

Laravel deserialise message

In my c# application I receive the following message from an external device.

Title: Color, Text: 6, Number: 0, Logic: false

How can I make properties from this string message?

Normally I deserialize it but in this case it's no json!

Upvotes: 0

Views: 53

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292635

Well, you can always fall back to good old manual parsing... Assuming the property values will never contain the character ,, you can do something like this:

static IDictionary<string, string> Parse(string input)
{
    var result = new Dictionary<string, string>();
    var pairs = input.Split(',');
    foreach (var pair in pairs)
    {
        var parts = pair.Split(new[] { ':' }, 2);
        string name = parts[0];
        string value = parts[1];
        result.Add(name, value);
    }
    return result;
}

(of course, if you want to deserialize it as an object you'll have to convert each value to the actual property type)

Upvotes: 1

Related Questions