public static void Write(this ICollection items, JsonWriter.Array aw) { var targets = aw.ParentProperty.SubProperties; if (targets != null && !targets.IsArray) throw new InvalidOperationException("Property's SubProperties must be an array."); if (targets == null)// || !targets.HasIndexProperties) { int i = 0; foreach (var item in (IEnumerable)items) { aw.WriteValue(new Property(i.ToString(), false, null), item); i++; } } else { //convert to IList so we can get the values by index var list = items as IList; if (list == null) list = new ArrayList(items); var defaultProperty = targets["-1"]; int max = Math.Min(targets.Max, list.Count - 1); for (int i = targets.Min; i <= max; i++) { var prop = targets[i.ToString()] ?? defaultProperty; if (prop != null) aw.WriteValue(prop, list[i]); } } }
public static void Write(TextWriter writer, bool ownsStream, Action<JsonWriter.Item> resolver, JsonWriterOptions options = null) { JsonWriter w = new JsonWriter(writer, options); if (options == null) options = defaultOptions; resolver(w.itemWriter); if (ownsStream) writer.Close(); }
public static void Write(this IDictionary items, JsonWriter.Object ow) { var targets = ow.ParentProperty.SubProperties; if (targets == null) foreach (DictionaryEntry item in items) { if (!(item.Key is IConvertible)) throw new InvalidOperationException("Cannot serialize Dictionary with non-IConvertable keys."); //var value = item.Value; //if (!value.TryAs<IDictionary>(d => ow.WriteObject(item.Key.ToString(), d.Write))) // if (value is IConvertible || !value.TryAs<ICollection>(c => ow.WriteArray(item.Key.ToString(), c.Write))) ow.WriteValue(new Property((string)item.Key), item.Value); } else foreach (var target in targets) { bool hasItem = items.Contains(target.Name); if (!hasItem && target.IsOptional) continue; object value = hasItem ? items[target.Name] : null; ow.WriteValue(target, value); } }
internal Object(JsonWriter writer) { this.writer = writer; }
internal Item(JsonWriter writer) { this.writer = writer; }
internal Array(JsonWriter writer) { this.writer = writer; }