/// <summary> /// Initializes a new instance of the <see cref="JsonFieldConverter"/> class. /// </summary> /// <param name="jsonFieldName">Name of the json field.</param> /// <param name="property">The property to map.</param> /// <param name="converter">Converter to change the data type when mapping.</param> /// <param name="type">The target JSON type.</param> public JsonFieldConverter(string jsonFieldName, ModelProperty property, IConverter converter, JsonFieldType type) { JsonFieldName = jsonFieldName; Property = property; Type = type; _converter = converter; }
/// <summary> /// Registers a JSON field for the model. /// </summary> protected JsonFieldConverter AddJsonField(string jsonFieldName, ModelProperty modelProperty, IConverter converter, JsonFieldType type) { var field = new JsonFieldConverter(jsonFieldName, modelProperty, converter, type); _jsonFields.Add(field); return(field); }
/// <summary> /// Initializes a new instance of the <see cref="JsonField"/> struct. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="type">The type.</param> /// <param name="value">The value.</param> public JsonField(string fieldName, JsonFieldType type, object value) { _fieldName = fieldName; _type = type; _value = value; }
/// <summary> /// Adds a field to the object. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="type">The type of the field.</param> /// <param name="value">The value of the field.</param> public void AddField(string fieldName, JsonFieldType type, object value) { var field = new JsonField(fieldName, type, value); _fields = _fields.AddOrUpdate(fieldName.ToLower(), field); }
private static void AppendValue(StringBuilder builder, object value, JsonFieldType type, int currentIndent, int indent) { if (value == null) { builder.Append("null"); return; } switch (type) { case JsonFieldType.Boolean: builder.Append((bool)value ? "true" : "false"); break; case JsonFieldType.Date: case JsonFieldType.DateTime: case JsonFieldType.String: builder.Append('"'); AppendString(builder, (string)value); builder.Append('"'); break; case JsonFieldType.Double: var scan = builder.Length; builder.Append(value); bool decimalFound = false; while (scan < builder.Length) { if (builder[scan] == '.') { decimalFound = true; break; } scan++; } if (!decimalFound) { builder.Append(".0"); } break; case JsonFieldType.Integer: builder.Append(value); break; case JsonFieldType.IntegerArray: builder.Append('['); var ints = (int[])value; for (int i = 0; i < ints.Length; i++) { if (i > 0) { builder.Append(','); } builder.Append(' '); builder.Append(ints[i]); } builder.Append(" ]"); break; case JsonFieldType.Object: ((JsonObject)value).AppendObject(builder, currentIndent, indent); break; case JsonFieldType.ObjectArray: builder.Append('['); if (indent > 0) { currentIndent += indent; AppendLine(builder, currentIndent, indent); } bool first = true; foreach (var nestedObject in ((IEnumerable <JsonObject>)value)) { if (first) { first = false; } else { builder.Append(','); if (indent > 0) { AppendLine(builder, currentIndent, indent); } } if (indent == 0) { builder.Append(' '); } nestedObject.AppendObject(builder, currentIndent, indent); } if (indent > 0) { currentIndent -= indent; AppendLine(builder, currentIndent, indent); } else { builder.Append(' '); } builder.Append(']'); break; default: throw new NotImplementedException("AppendValue(" + type + ")"); } }
/// <summary> /// Initializes a new instance of the <see cref="JsonRelatedFieldConverter"/> class. /// </summary> /// <param name="jsonFieldName">Name of the json field.</param> /// <param name="type">Type of the json field.</param> /// <param name="getValue">Method to retrieve the mapped data.</param> public JsonRelatedFieldConverter(string jsonFieldName, JsonFieldType type, Func <ModelBase, IDataModelSource, object> getValue) : base(jsonFieldName, null, null, type) { _getValue = getValue; }