bas
bas

Reputation: 14912

NewtonSoft JsonConvert list

I have a very simple structure I would like to serialize using newton soft Json serialization.

The definition:

public enum SensorType
{
    Temperature,
    Flow,
    Pressure
}

public enum SensorLocation
{
    Manifold,
    TopVessel,
    WaferStage
}

[JsonArray]
public class SensorConfiguration
{
    [JsonProperty]
    public string Name { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public SensorType Type { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public SensorLocation Location { get; set; }

    public SensorConfiguration()
    {
    }

    public SensorConfiguration(string name, SensorType type, SensorLocation location)
    {
        Name = name;
        Type = type;
        Location = location;
    }
}

The serialization:

        var topvessel = Sensors.TopVessel.Select(sensor =>
            new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.TopVessel));
        var manifold = Sensors.Manifold.Select(sensor =>
            new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.Manifold));
        var waferstage = Sensors.WaferStage.Select(sensor =>
            new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.Manifold));

        var sensorConfigurations = topvessel.Concat(manifold).Concat(waferstage).ToList();

        var json = JsonConvert.SerializeObject(sensorConfigurations);

The error:

System.InvalidCastException : Unable to cast object of type 'Asml.Mbi.FlowAndTemperature.Interfaces.Configuration.SensorConfiguration' to type 'System.Collections.IEnumerable'.
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) 
    at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
    at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
    at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
    at Asml.Mbi.FlowAndTemperature.Peripherals.Ftcb.FtcBox.GetSensorConfiguration() in D:\dev\multibeaminspection\BuildingBlocks\FlowAndTemperature\Implementation\Peripherals\Ftcb\FtcBox.cs:line 75
    at Asml.Mbi.FlowAndTemperature.UnitTest.FtcBoxTests.GetConfiguration() in D:\dev\multibeaminspection\BuildingBlocks\FlowAndTemperature\UnitTest\FtcBoxTests.cs:line 212

What am I doing wrong? The example shows it's possible...

Upvotes: 1

Views: 351

Answers (1)

Mr.Deer
Mr.Deer

Reputation: 476

Try to remove [JsonArray]

so you code would look like

public class SensorConfiguration
{
    [JsonProperty]
    public string Name { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public SensorType Type { get; set; }
    [JsonConverter(typeof(StringEnumConverter))]
    public SensorLocation Location { get; set; }

    public SensorConfiguration()
    {
    }

    public SensorConfiguration(string name, SensorType type, SensorLocation location)
    {
        Name = name;
        Type = type;
        Location = location;
    }
}

Upvotes: 3

Related Questions