using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(ControlBaseConverter))]
public abstract class ControlBase
public Editor editor { get; set; }
public string alias { get; set; }
public class CallToActionValue
public string name { get; set; }
public string url { get; set; }
public string target { get; set; }
public string icon { get; set; }
public class CallToAction
public List<CallToActionValue> value { get; set; }
public string dataTypeGuid { get; set; }
public string editorAlias { get; set; }
public string editorName { get; set; }
public class CallToActionControlValue
public CallToAction callToAction { get; set; }
[JsonConverter(typeof(NoConverter))]
public class CallToActionControl : ControlBase
public List<CallToActionControlValue> value { get; set; }
public string guid { get; set; }
[JsonConverter(typeof(NoConverter))]
public class RteControl : ControlBase
public string value { get; set; }
public List<ControlBase> controls { get; set; }
class ControlBaseConverter : JsonConverter
public override bool CanConvert(Type objectType)
return objectType == typeof(ControlBase);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
var obj = JObject.Load(reader);
var editorValue = (string)obj.SelectToken("editor.alias");
if (string.Equals(editorValue, "rte", StringComparison.OrdinalIgnoreCase))
type = typeof(RteControl);
else if (string.Equals(editorValue, "callToAction", StringComparison.OrdinalIgnoreCase))
type = typeof(CallToActionControl);
throw new JsonSerializationException("Unknown type of Control: " + editorValue);
return obj.ToObject(type, serializer);
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public class NoConverter : JsonConverter
public override bool CanConvert(Type objectType)
public override bool CanRead { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
throw new NotImplementedException();
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public static class TestClass
public static void Test()
var jsonString = GetJson();
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine("Deserialized and re-serialized JSON:" );
Console.WriteLine(json2);
if (JToken.DeepEquals(JToken.Parse(jsonString), JToken.Parse(json2)))
Console.WriteLine("Deserialized and re-serialized JSON are identical." );
throw new InvalidOperationException("Error: Deserialized and re-serialized JSON are NOT identical.");
""value"":""<p>The Governments current tobacco control strategy Creating a Tobacco-Free Generation: A Tobacco Control Strategy for Scotland sets out the Governments vision for a tobacco-free Scotland by 2034. Our contribution to this strategy is through</p>\n<ul>\n<li>prevention</li>\n<li>protection</li>\n<li>smoking cessation services.</li>\n</ul>\n<p>Tobacco smoking is a major risk factor for</p>\n<ul>\n<li>coronary heart disease</li>\n<li>stroke</li>\n<li>peripheral vascular disease</li>\n<li>many respiratory conditions including COPD</li>\n<li>a range of cancers </li>\n</ul>\n<p>and many other diseases and conditions. It is a leading cause of preventable ill health, premature death and disability.</p>\n<p>The Governments strategy outlines the importance of reducing health inequalities and the role that reducing smoking rates in the most deprived communities can have on this.</p>"",
""name"":""Read Government’s tobacco control strategy (external website)"",
""url"":""http://www.gov.gov/Publications"",
""dataTypeGuid"":""5befdwe546fb12-3bc2-4f39-bab3-7328dfg345382afe65"",
""editorAlias"":""callToAction"",
""alias"":""callToAction""
""guid"":""e3as34dc2831-c4e8-8671-038c-15634see45de7b71a""
""value"":""<h2>Reducing tobacco-related inequalities </h2>\n<p>The European Regional Office of the World Health Organisation (WHO) has produced policy guidance focusing on reducing inequalities relating to tobacco smoking. The guidance looks at</p>\n<ul>\n<li>the widening socio-economic inequities in tobacco consumption</li>\n<li>how the use of tobacco contributes to health inequalities</li>\n<li>the complexity of smoking in terms of inequality</li>\n<li>how tobacco increases inequalities over the life course</li>\n<li>what tobacco policies need to consider to address inequalities.</li>\n</ul>\n<p>The guidance reports that</p>\n<ul>\n<li>A child born in a more socially deprived area of Scotland is more likely to be born into a family that smokes, is more likely to have a mother who smoked during her pregnancy and is more likely to be around smokers growing up. Children of smokers are more likely to start smoking themselves and continue to repeat the cycle of their own experience.</li>\n<li>People living in the most deprived areas are less likely to feel in control of their life, more likely to experience stress and mental health issues and be less aware of the harm of smoking and second hand smoke.</li>\n<li>When it comes to stopping smoking this group of smokers are more likely to smoke heavily and have a stronger nicotine dependence and therefore find it harder to stop. They are less likely to know where to get help, and have less encouragement and social support to quit.</li>\n<li>They are more likely to suffer financial hardship as a consequence of smoking related illness, more likely to live in poor housing and more likely to have other health problems made worse by smoking.</li>\n</ul>\n<p>You can read the full <a href=\""http://ec.europa.eu/health/social_determinants/key_documents/index_en.htm#anchor1\"">‘Tobacco and inequities’ guidance</a> on the European Commission’s website (external website).</p>\n<p>Our tobacco control work is achieved by working in partnership with Government, local and national NHS Boards along with <a href=\""http://www.ashscotland.org.uk/\"" target=\""_blank\"">ASH Scotland</a> (external website) including the <a href=\""http://www.ashscotland.org.uk/alliances\"" target=\""_blank\""> Tobacco-free Alliance</a> (external website).</p>\n<h2>Getting help with local tobacco planning</h2>\n<p>To help with local planning, Public Health Observatory () have created local tobacco control profiles which include indicators on </p>\n<ul>\n<li>adult smoking prevalence</li>\n<li>smoking during and post-pregnancy</li>\n<li>smoking cessation</li>\n<li>smoking related death and disease</li>\n<li>tobacco sales.</li>\n</ul>"",
""name"":""Visit the TEST online profiles tool (external website)"",
""url"":""http://www.TEST.org.uk/comparative-health/profiles/online-profiles-tool"",
""dataTypeGuid"":""5bs3a232-3bc2-4f39-bab3-73283ewr82afe65"",
""editorAlias"":""callToAction"",
""alias"":""callToAction""
""guid"":""25ese341-4c05-0c11-e643-3f3aderd84e8e3""
public static void Main()
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);