in Agents/Xamarin.Interactive/Representations/RepresentationManager.cs [200:281]
object Normalize (object obj)
{
switch (obj) {
case null:
return null;
case Representation originalRepresentation:
// an object may be boxed in a Representation object if it has metadata
// associated with it, such as whether the representation provider supports
// "editing" via TryConvertFromRepresentation. We must still normalize the
// value inside to ensure we can safely serialize it. If the value differs
// after normalization, but is serializable, we re-box it with the normalized
// value and the canEdit flag unset.
var normalizedRepresentationValue = Normalize (originalRepresentation.Value);
if (Equals (originalRepresentation.Value, normalizedRepresentationValue))
return obj;
if (normalizedRepresentationValue == null)
return null;
return originalRepresentation.With (
normalizedRepresentationValue,
canEdit: false);
}
if (agentRepresentationProvider != null) {
try {
var normalized = agentRepresentationProvider.NormalizeRepresentation (obj);
if (normalized != null)
return normalized;
} catch (Exception e) {
Log.Error (TAG, "Agent-builtin normalizer raised an exception", e);
}
}
switch (obj) {
case Enum enumValue:
return new EnumValue (enumValue);
case IInteractiveObject interactive:
interactive.Handle = ObjectCache.Shared.GetHandle (interactive);
return interactive;
case Exception exception:
return ExceptionNode.Create (exception);
case Type asType:
return TypeNode.Create (asType);
case MemberInfo memberInfo:
try {
var remoteMemberInfo = TypeMember.Create (memberInfo);
if (remoteMemberInfo != null)
return remoteMemberInfo;
} catch (Exception e) {
Log.Warning (TAG, "unable to create TypeMember from MemberInfo", e);
}
return null;
case TimeSpan _:
case Guid _:
return obj;
case IntPtr intptr:
return new WordSizedNumber (
obj,
WordSizedNumberFlags.Pointer | WordSizedNumberFlags.Signed,
(ulong)intptr);
case UIntPtr uintptr:
return new WordSizedNumber (
obj,
WordSizedNumberFlags.Pointer,
(ulong)uintptr);
default:
var type = obj.GetType ();
if (type.GetCustomAttribute<JsonObjectAttribute> () != null)
return obj;
if (Type.GetTypeCode (type) != TypeCode.Object)
return obj;
if (obj is byte [])
return obj;
return null;
}
}