我有一个非常简单的结构,我想使用牛顿软Json序列化进行序列化。
定义:
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;
}
}
序列化:
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);
错误:
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
我在做什么错? example表示有可能...
答案 0 :(得分:3)
尝试删除[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;
}
}