Hi, I am trying to figure out the best way to setup a WCF DataContract for calls to the web service. I am confused because when I get a list of lights from the http api, I try to deserialize it into my C# WCF DataContract and it won’t work because my color property is a string so that I can use the DataContract to update the state of my lights as well. Do I have to use a completely separate DataContract for the update calls? I am trying to make my code as compact and streamlined as possible, but the color values being different types based on the type of api call makes that very difficult. I appreciate any help that you can provide. Below is my LightData.cs file to show what I am attempting to accomplish:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
namespace HelloWorld
{
public class Color
{
public double hue { get; set; }
public double saturation { get; set; }
public int kelvin { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public string ToRGBString() { return "rgb:" + R.ToString() + "," + G.ToString() + "," + B.ToString(); } }
public class Group { public string id { get; set; } public string name { get; set; } }
public class Location { public string id { get; set; } public string name { get; set; } }
public class Capabilities { public bool has_color { get; set; } public bool has_variable_color_temp { get; set; } }
public class Product { public string name { get; set; } public string company { get; set; } public string identifier { get; set; } public Capabilities capabilities { get; set; } }
[DataContract] public class LightData { //Memory stream and serialization objects private MemoryStream ms = new MemoryStream(); private DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LightData));
public string ToJSON() { ser.WriteObject(ms, this); //Serialize this class and write it to the memory stream ms.Position = 0; //Reset the memory stream pointer StreamReader sr = new StreamReader(ms); return sr.ReadToEnd(); //Read the memory stream and return the resulting value }
public LightData ToObject() { ms.Position = 0; //Reset the memory stream pointer LightData ldObj = (LightData)ser.ReadObject(ms); //Deserialize the memory stream, return an object and convert that object to a LightData object return ldObj; } [DataMember] public string id { get; set; }
[DataMember] public string uuid { get; set; }
[DataMember] public string label { get; set; }
[DataMember] public bool connected { get; set; }
[DataMember] public string power { get; set; }
[DataMember] public Color color { get; set; }
[DataMember] public double brightness { get; set; }
[DataMember] public Group group { get; set; }
[DataMember] public Location location { get; set; }
[DataMember] public string last_seen { get; set; }
[DataMember] public double seconds_since_seen { get; set; }
[DataMember] public Product product { get; set; } }
[CollectionDataContract] public class LightCollection : ICollection<LightData> { public List<LightData> Lights = new List<LightData>();
private MemoryStream ms = new MemoryStream(); private DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LightCollection));
public void Add(LightData ld) { Lights.Add(ld); }
public void Clear() { Lights = null; }
public bool Contains(LightData ld) { if (Lights.Contains(ld)) { return true; } else { return false; } }
public void CopyTo(LightData[] ldArray, int index) { Lights.CopyTo(ldArray, index); }
public bool Remove(LightData ld) { return Lights.Remove(ld); }
public int Count { get; set; }
public bool IsReadOnly { get; set; }
public IEnumerator<LightData> GetEnumerator() { return Lights.GetEnumerator(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
public string ToJSON() { ser.WriteObject(ms, this); ms.Position = 0; StreamReader sr = new StreamReader(ms); return sr.ReadToEnd(); }
public LightCollection FromJSON(string json) { MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); ms.Position = 0; LightCollection Lights = (LightCollection)ser.ReadObject(ms); ms.Dispose(); return Lights; } }
}