private (JToken first, JObject second) buildNode(ITypedElement node) { var details = node.GetJsonSerializationDetails(); object value = node.Definition != null ? node.Value : details?.OriginalValue ?? node.Value; var objectInShadow = node.InstanceType != null?Primitives.IsPrimitive(node.InstanceType) : details.UsesShadow; JToken first = value != null?buildValue(value) : null; JObject second = buildChildren(node); // If this is a complex type with a value (should not occur) // serialize it like a primitive, otherwise, the first member // is just the normal content of the complex type (the children) if (!objectInShadow && first == null) { if (first == null) { first = second; second = null; } } return(first, second); JObject buildChildren(ITypedElement n) { var objectWithChildren = new JObject(); addChildren(n, objectWithChildren); return(objectWithChildren.Count == 0 ? null : objectWithChildren); } }