/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } Enum e = (Enum)value; string enumName = e.ToString("G"); if (char.IsNumber(enumName[0]) || enumName[0] == '-') { writer.WriteValue(value); } else { BidirectionalDictionary <string, string> map = GetEnumNameMap(e.GetType()); string resolvedEnumName; map.TryGetByFirst(enumName, out resolvedEnumName); resolvedEnumName = resolvedEnumName ?? enumName; if (CamelCaseText) { resolvedEnumName = StringUtils.ToCamelCase(resolvedEnumName); } writer.WriteValue(resolvedEnumName); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } Enum @enum = (Enum)value; string text = @enum.ToString("G"); if (char.IsNumber(text[0]) || text[0] == '-') { writer.WriteValue(value); } else { BidirectionalDictionary <string, string> enumNameMap = this.GetEnumNameMap(@enum.GetType()); string text2; enumNameMap.TryGetByFirst(text, out text2); text2 = (text2 ?? text); if (this.CamelCaseText) { text2 = StringUtils.ToCamelCase(text2); } writer.WriteValue(text2); } }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } System.Enum e = (System.Enum)value; string EnumName = e.ToString("G"); if (char.IsNumber(EnumName[0]) || EnumName[0] == '-') { writer.WriteValue(value); } else { BidirectionalDictionary <string, string> map = this.GetEnumNameMap(e.GetType()); string resolvedEnumName; map.TryGetByFirst(EnumName, out resolvedEnumName); resolvedEnumName = resolvedEnumName ?? EnumName; if (this.CamelCaseText) { string[] names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())).ToArray(); resolvedEnumName = string.Join(", ", names); } writer.WriteValue(resolvedEnumName); } }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } Enum e = (Enum)value; string enumName = e.ToString("G"); if (char.IsNumber(enumName[0]) || enumName[0] == '-') { // enum value has no name so write number writer.WriteValue(value); } else { BidirectionalDictionary <string, string> map = EnumMemberNamesPerType.Get(e.GetType()); string[] names = enumName.Split(','); for (int i = 0; i < names.Length; i++) { string name = names[i].Trim(); string resolvedEnumName; map.TryGetByFirst(name, out resolvedEnumName); resolvedEnumName = resolvedEnumName ?? name; if (CamelCaseText) { resolvedEnumName = StringUtils.ToCamelCase(resolvedEnumName); } names[i] = resolvedEnumName; } string finalName = string.Join(", ", names); writer.WriteValue(finalName); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } Enum @enum = (Enum)value; string text = @enum.ToString("G"); if (char.IsNumber(text[0]) || text[0] == '-') { writer.WriteValue(value); return; } BidirectionalDictionary <string, string> enumNameMap = this.GetEnumNameMap(@enum.GetType()); string[] array = text.Split(new char[] { ',' }); for (int i = 0; i < array.Length; i++) { string text2 = array[i].Trim(); string text3; enumNameMap.TryGetByFirst(text2, out text3); text3 = (text3 ?? text2); if (this.CamelCaseText) { text3 = StringUtils.ToCamelCase(text3); } array[i] = text3; } string value2 = string.Join(", ", array); writer.WriteValue(value2); }