private bool ReflectionTryWritePrimitiveArray(JsonWriterDelegator jsonWriter, object obj, Type underlyingType, Type itemType, XmlDictionaryString collectionItemName) { PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(itemType); if (primitiveContract == null) { return(false); } XmlDictionaryString itemNamespace = null; switch (itemType.GetTypeCode()) { case TypeCode.Boolean: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonBooleanArray((bool[])obj, collectionItemName, itemNamespace); break; case TypeCode.DateTime: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDateTimeArray((DateTime[])obj, collectionItemName, itemNamespace); break; case TypeCode.Decimal: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDecimalArray((decimal[])obj, collectionItemName, itemNamespace); break; case TypeCode.Int32: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonInt32Array((int[])obj, collectionItemName, itemNamespace); break; case TypeCode.Int64: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonInt64Array((long[])obj, collectionItemName, itemNamespace); break; case TypeCode.Single: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonSingleArray((float[])obj, collectionItemName, itemNamespace); break; case TypeCode.Double: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDoubleArray((double[])obj, collectionItemName, itemNamespace); break; default: return(false); } return(true); }
public void ReflectionWriteCollection(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, CollectionDataContract collectionContract) { JsonWriterDelegator jsonWriter = xmlWriter as JsonWriterDelegator; if (jsonWriter == null) { throw new ArgumentException(nameof(xmlWriter)); } XmlDictionaryString itemName = context.CollectionItemName; if (collectionContract.Kind == CollectionKind.Array) { context.IncrementArrayCount(jsonWriter, (Array)obj); Type itemType = collectionContract.ItemType; if (!ReflectionTryWritePrimitiveArray(jsonWriter, obj, collectionContract.UnderlyingType, itemType, itemName)) { ReflectionWriteArrayAttribute(jsonWriter); Array array = (Array)obj; PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(itemType); for (int i = 0; i < array.Length; ++i) { _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, itemName); _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, itemType, array.GetValue(i), false, primitiveContract); _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } } else { collectionContract.IncrementCollectionCount(jsonWriter, obj, context); IEnumerator enumerator = collectionContract.GetEnumeratorForCollection(obj); bool canWriteSimpleDictionary = collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary; bool useSimpleDictionaryFormat = context.UseSimpleDictionaryFormat; if (canWriteSimpleDictionary && useSimpleDictionaryFormat) { ReflectionWriteObjectAttribute(jsonWriter); Type[] itemTypeGenericArguments = collectionContract.ItemType.GetGenericArguments(); Type dictionaryValueType = itemTypeGenericArguments.Length == 2 ? itemTypeGenericArguments[1] : null; while (enumerator.MoveNext()) { object current = enumerator.Current; object key = ((IKeyValue)current).Key; object value = ((IKeyValue)current).Value; _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, key.ToString()); _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, dictionaryValueType ?? value.GetType(), value, false, primitiveContractForParamType: null); _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } else { ReflectionWriteArrayAttribute(jsonWriter); PrimitiveDataContract primitiveContractForType = PrimitiveDataContract.GetPrimitiveDataContract(collectionContract.UnderlyingType); if (primitiveContractForType != null && primitiveContractForType.UnderlyingType != Globals.TypeOfObject) { while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); primitiveContractForType.WriteXmlElement(jsonWriter, current, context, itemName, null /*namespace*/); } } else { Type elementType = collectionContract.GetCollectionElementType(); bool isDictionary = collectionContract.Kind == CollectionKind.Dictionary || collectionContract.Kind == CollectionKind.GenericDictionary; DataContract itemContract = null; JsonDataContract jsonDataContract = null; if (isDictionary) { itemContract = XmlObjectSerializerWriteContextComplexJson.GetRevisedItemContract(collectionContract.ItemContract); jsonDataContract = JsonDataContract.GetJsonDataContract(itemContract); } while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, itemName); if (isDictionary) { jsonDataContract.WriteJsonValue(jsonWriter, current, context, collectionContract.ItemType.TypeHandle); } else { _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, elementType, current, false, primitiveContractForParamType: null); } _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } } } }
private bool ReflectionTryWritePrimitiveArray(JsonWriterDelegator jsonWriter, object obj, Type underlyingType, Type itemType, XmlDictionaryString collectionItemName) { PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(itemType); if (primitiveContract == null) return false; XmlDictionaryString itemNamespace = null; switch (itemType.GetTypeCode()) { case TypeCode.Boolean: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonBooleanArray((bool[])obj, collectionItemName, itemNamespace); break; case TypeCode.DateTime: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDateTimeArray((DateTime[])obj, collectionItemName, itemNamespace); break; case TypeCode.Decimal: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDecimalArray((decimal[])obj, collectionItemName, itemNamespace); break; case TypeCode.Int32: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonInt32Array((int[])obj, collectionItemName, itemNamespace); break; case TypeCode.Int64: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonInt64Array((long[])obj, collectionItemName, itemNamespace); break; case TypeCode.Single: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonSingleArray((float[])obj, collectionItemName, itemNamespace); break; case TypeCode.Double: ReflectionWriteArrayAttribute(jsonWriter); jsonWriter.WriteJsonDoubleArray((double[])obj, collectionItemName, itemNamespace); break; default: return false; } return true; }