internal object Deserialize(Type nativeType) { if (nativeType == null) { throw Error.ArgumentNull(nameof(nativeType)); } object primitiveValue = _current.Value; if (nativeType.IsEnum()) { return(primitiveValue); } try { if (primitiveValue is PartialDateTime || primitiveValue is PartialTime) { return(PrimitiveTypeConverter.ConvertTo(primitiveValue.ToString(), nativeType)); } else { return(PrimitiveTypeConverter.ConvertTo(primitiveValue, nativeType)); } } catch (NotSupportedException exc) { // thrown when an unsupported conversion was required ComplexTypeReader.RaiseFormatError("Not supported - " + exc.Message, _current.Location); throw; // just to satisfy the compiler - RaiseFormatError throws. } }
internal object Deserialize(Type nativeType) { if (nativeType == null) { throw Error.ArgumentNull(nameof(nativeType)); } object primitiveValue = _current.Value; if (nativeType.IsEnum()) { return(primitiveValue); } try { // The POCO's know nothing about the special partial date/time classes used by ITypedElement, // instead FhirDateTime, Time and FhirDate all represent these values as simple strings. if (primitiveValue is P.DateTime || primitiveValue is P.Time || primitiveValue is P.Date) { return(PrimitiveTypeConverter.ConvertTo(primitiveValue.ToString(), nativeType)); } else { return(PrimitiveTypeConverter.ConvertTo(primitiveValue, nativeType)); } } catch (NotSupportedException exc) { // thrown when an unsupported conversion was required ComplexTypeReader.RaiseFormatError("Not supported - " + exc.Message, _current.Location); throw; // just to satisfy the compiler - RaiseFormatError throws. } }
#pragma warning restore 612,618 public object Deserialize(PropertyMapping prop, string memberName, object existing = null) { if (prop == null) { throw Error.ArgumentNull(nameof(prop)); } // ArrayMode avoid the dispatcher making nested calls into the RepeatingElementReader again // when reading array elements. FHIR does not support nested arrays, and this avoids an endlessly // nesting series of dispatcher calls if (!_arrayMode && prop.IsCollection) { if (existing != null && !(existing is IList)) { throw Error.Argument(nameof(existing), "Can only read repeating elements into a type implementing IList"); } var reader = new RepeatingElementReader(_inspector, _current, Settings); return(reader.Deserialize(prop, memberName, (IList)existing)); } // If this is a primitive type, no classmappings and reflection is involved, // just parse the primitive from the input // NB: no choices for primitives! if (prop.IsPrimitive) { var reader = new PrimitiveValueReader(_current); return(reader.Deserialize(prop.ImplementingType)); } // A Choice property that contains a choice of any resource // (as used in Resource.contained) if (prop.Choice == ChoiceType.ResourceChoice) { var reader = new ComplexTypeReader(_inspector, _current, Settings); return(reader.Deserialize(null)); } if (_current.InstanceType is null) { ComplexTypeReader.RaiseFormatError( "Underlying data source was not able to provide the actual instance type of the resource.", _current.Location); } ClassMapping mapping = prop.Choice == ChoiceType.DatatypeChoice ? getMappingForType(memberName, _current.InstanceType) : _inspector.ImportType(prop.ImplementingType); // Handle other Choices having any datatype or a list of datatypes if (existing != null && !(existing is Resource) && !(existing is Element)) { throw Error.Argument(nameof(existing), "Can only read complex elements into types that are Element or Resource"); } var cplxReader = new ComplexTypeReader(_inspector, _current, Settings); return(cplxReader.Deserialize(mapping, (Base)existing)); }
private ClassMapping getMappingForType(string memberName, string typeName) { // NB: this will return the latest type registered for that name, so supports type mapping/overriding // Maybe we should Import the types present on the choice, to make sure they are available. For now // assume the caller has Imported all types in the right (overriding) order. ClassMapping result = _inspector.FindClassMapping(typeName); if (result == null) { ComplexTypeReader.RaiseFormatError( $"Encountered polymorph member {memberName}, which uses unknown datatype {typeName}", _current.Location); } return(result); }
#pragma warning restore 612,618 public Resource Deserialize(Resource existing = null) { if (_reader.InstanceType is null) { ComplexTypeReader.RaiseFormatError( "Underlying data source was not able to provide the actual instance type of the resource.", _reader.Location); } var mapping = _inspector.FindClassMappingForResource(_reader.InstanceType); if (mapping == null) { ComplexTypeReader.RaiseFormatError($"Asked to deserialize unknown resource '{_reader.InstanceType}'", _reader.Location); } // Delegate the actual work to the ComplexTypeReader, since // the serialization of Resources and ComplexTypes are virtually the same var cplxReader = new ComplexTypeReader(_reader, Settings); return((Resource)cplxReader.Deserialize(mapping, existing)); }