using System.Collections.Generic;
interface IAngularExpression
class AngularExpression : IAngularExpression
private readonly string _code;
public AngularExpression(string code)
public override string ToString()
class AngularConverter : JsonConverter
public override bool CanConvert(Type objectType)
return typeof(IAngularExpression).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
throw new NotImplementedException();
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
writer.WriteRawValue(value.ToString());
public static void Main()
var obj = new Dictionary<string, object> {
{ "date", DateTime.Now },
{ "myValue", new AngularExpression("argh") },
{ "myCode", new AngularExpression("() => console.log('')") },
{ "child", new Dictionary<string, object> {
var sb = new StringBuilder();
using (var stringWriter = new StringWriter(sb))
using (var jsonWriter = new JsonTextWriter(stringWriter))
jsonWriter.QuoteChar = '\'';
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
jsonWriter.QuoteName = false;
var serializer = new JsonSerializer();
serializer.Converters.Add(new AngularConverter());
serializer.Serialize(jsonWriter, obj);
Console.WriteLine(sb.ToString());