serializeObject method

dynamic serializeObject (
  1. Object object,
  2. SerializationContext context
)

Implementation

dynamic serializeObject(Object object, SerializationContext context) {
  if (object == null) {
    return object;
  }

  final im = safeGetInstanceMirror(object);
  final converter =
      getConverter(context.parentMeta, object.runtimeType, null, im);
  if (converter != null) {
    configureConverter(converter,
        value: object, serializationContext: context);
    var convertedValue = converter.toJSON(object, null);
    if (object is Iterable && convertedValue == object) {
      convertedValue = serializeIterable(object, context);
    }
    return convertedValue;
  }

  if (object is Iterable) {
    return serializeIterable(object, context);
  }

  if (im == null || im.type == null) {
    if (im != null) {
      throw MissingEnumValuesError(object.runtimeType);
    } else {
      if (context.options.ignoreUnknownTypes == true) {
        return null;
      } else {
        throw MissingAnnotationOnTypeError(object.runtimeType);
      }
    }
  }

  final classInfo = ClassInfo(im.type);
  final jsonMeta = classInfo.getMeta(context.options.scheme);
  final initialMap = context.options.template ?? <String, dynamic>{};
  final result = JsonMap(initialMap, jsonMeta);
  final processedObjectDescriptor = getObjectProcessed(object, context.level);
  if (processedObjectDescriptor != null &&
      processedObjectDescriptor.levelsCount > 1) {
    final allowanceIsSet =
        (jsonMeta != null && jsonMeta.allowCircularReferences > 0);
    final allowanceExceeded = (allowanceIsSet &&
            processedObjectDescriptor.levelsCount >
                jsonMeta.allowCircularReferences)
        ? true
        : null;
    if (allowanceExceeded == true) {
      return null;
    }
    if (allowanceIsSet == false) {
      throw CircularReferenceError(object);
    }
  }
  dumpTypeNameToObjectProperty(result, im.type, context.options);
  enumeratePublicProperties(im, null, context.options, (name,
      jsonName,
      value,
      isGetterOnly,
      JsonProperty meta,
      converter,
      scalarType,
      TypeInfo typeInfo) {
    if (value == null && meta != null && meta.defaultValue != null) {
      result.setPropertyValue(jsonName, meta.defaultValue);
    } else {
      var convertedValue;
      final newContext =
          SerializationContext(context.options, context.level + 1, meta);
      if (converter != null) {
        configureConverter(converter,
            value: value, serializationContext: context);
        final valueTypeInfo = getTypeInfo(value.runtimeType);
        dynamic convert(item) => converter.toJSON(item, meta);
        if (valueTypeInfo.isIterable) {
          convertedValue = converter.toJSON(value, meta);
          if (convertedValue == value) {
            convertedValue = serializeIterable(value, newContext);
          }
        } else {
          convertedValue = convert(value);
        }
      } else {
        convertedValue = serializeObject(value, newContext);
      }
      result.setPropertyValue(jsonName, convertedValue);
    }
  });

  final jsonAnyGetter = classInfo.getJsonAnyGetter();
  if (jsonAnyGetter != null) {
    final anyMap = im.invoke(jsonAnyGetter.simpleName, []);
    result.map.addAll(anyMap);
  }

  return result.map;
}