Пример #1
0
        public override void WriteJson(JsonWriter writer, [AllowNull] Entity entity, JsonSerializer serializer)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (serializer is null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity), "Entity can't be empty.");
            }

            var md = _metadata.GetEntityMetadata(entity.LogicalName);

            if (!entity.ContainsValue(md.PrimaryIdAttribute) && !Guid.Equals(entity.Id, Guid.Empty))
            {
                entity[md.PrimaryIdAttribute] = entity.Id;
            }

            writer.WriteStartObject();
            foreach (var(attributeName, attributeValue) in entity.Attributes)
            {
                var propName  = attributeName;
                var propValue = attributeValue;

                if (propValue is EntityReference)
                {
                    propName = $"{attributeName}@odata.bind";
                }

                writer.WritePropertyName(propName);

                if (attributeValue is DateTime dateTime &&
                    _metadata.IsDateOnlyAttribute(entity.LogicalName, propName))
                {
                    propValue = dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                }

                serializer.Serialize(writer, propValue);
            }
            writer.WriteEndObject();

            //serializer.Serialize(writer, entity.Attributes);
        }
Пример #2
0
        public override void WriteJson(JsonWriter writer, [AllowNull] Entity entity, JsonSerializer serializer)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (serializer is null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity), "Entity can't be empty.");
            }

            var md = _metadata.GetEntityMetadata(entity.LogicalName);

            if (!entity.ContainsValue(md.PrimaryIdAttribute) && !Guid.Equals(entity.Id, Guid.Empty))
            {
                entity[md.PrimaryIdAttribute] = entity.Id;
            }

            writer.WriteStartObject();

            // ODataType
            writer.WritePropertyName("@odata.type");
            serializer.Serialize(writer, $"Microsoft.Dynamics.CRM.{entity.LogicalName}");

            foreach (var(attributeName, attributeValue) in entity.Attributes)
            {
                var propName  = attributeName;
                var propValue = attributeValue;

                if (propValue is EntityReference propValueRef)
                {
                    var relationMd = _metadata.GetRelationshipMetadata(rel =>
                                                                       rel.ReferencingEntity.Equals(entity.LogicalName, StringComparison.OrdinalIgnoreCase) &&
                                                                       rel.ReferencingAttribute.Equals(attributeName, StringComparison.OrdinalIgnoreCase) &&
                                                                       rel.ReferencedEntity.Equals(propValueRef.LogicalName));

                    if (!string.IsNullOrEmpty(relationMd?.ReferencingEntityNavigationPropertyName))
                    {
                        propName = $"{relationMd.ReferencingEntityNavigationPropertyName}@odata.bind";
                    }
                    else
                    {
                        propName = $"{attributeName}@odata.bind";
                    }
                }

                writer.WritePropertyName(propName);

                if (attributeValue is DateTime dateTime &&
                    _metadata.IsDateOnlyAttribute(entity.LogicalName, propName))
                {
                    propValue = dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                }

                serializer.Serialize(writer, propValue);
            }
            writer.WriteEndObject();

            //serializer.Serialize(writer, entity.Attributes);
        }