Exemplo n.º 1
0
        public Expression HandleReference(object value, JsonPath CurrentPath)
        {
            if (!IsReferenceable(value))
            {
                return(null);
            }

            ReferenceInfo refInfo = null;

            if (_refs.ContainsKey(value))
            {
                /*
                 * This object has already been seen by the serializer so
                 * determine what to do with it.  If its part of the current path
                 * then its a circular reference and an error needs to be thrown or it should
                 * be ignored depending on the option. Otherwise write a reference to it
                 */
                refInfo = _refs[value];
                JsonPath refPath = refInfo.Path;
                switch (_context.ReferenceWritingType)
                {
                case SerializationContext.ReferenceOption.WriteIdentifier:
                    if (!refInfo.CanReference)
                    {
                        throw new InvalidOperationException("Can't reference object: " + refPath + " from " + CurrentPath + ", either it is a collection, or it has not been converted yet");
                    }

                    return(new ReferenceExpression(refPath));

                case SerializationContext.ReferenceOption.IgnoreCircularReferences:
                    if (CurrentPath.StartsWith(refPath))
                    {
                        return(new NullExpression());
                    }
                    break;

                case SerializationContext.ReferenceOption.ErrorCircularReferences:
                    if (CurrentPath.StartsWith(refPath))
                    {
                        throw new InvalidOperationException("Circular reference detected.  Current path: " + CurrentPath + ", reference to: " + refPath);
                    }
                    break;
                }
            }
            else
            {
                refInfo      = new ReferenceInfo(CurrentPath);
                _refs[value] = refInfo;
            }
            return(null);
        }
        public Expression HandleReference(object value, JsonPath CurrentPath)
        {
            if (!IsReferenceable(value))
                return null;

            ReferenceInfo refInfo = null;
            if (_refs.ContainsKey(value))
            {
                /*
                 * This object has already been seen by the serializer so
                 * determine what to do with it.  If its part of the current path
                 * then its a circular reference and an error needs to be thrown or it should
                 * be ignored depending on the option. Otherwise write a reference to it
                 */
                refInfo = _refs[value];
                JsonPath refPath = refInfo.Path;
                switch (_context.ReferenceWritingType)
                {
                    case SerializationContext.ReferenceOption.WriteIdentifier:
                        if (!refInfo.CanReference)
                            throw new InvalidOperationException("Can't reference object: " + refPath + " from " + CurrentPath + ", either it is a collection, or it has not been converted yet");

                        return new ReferenceExpression(refPath);
                    case SerializationContext.ReferenceOption.IgnoreCircularReferences:
                        if (CurrentPath.StartsWith(refPath))
                        {
                            return new NullExpression();
                        }
                        break;
                    case SerializationContext.ReferenceOption.ErrorCircularReferences:
                        if (CurrentPath.StartsWith(refPath))
                        {
                            throw new InvalidOperationException("Circular reference detected.  Current path: " + CurrentPath + ", reference to: " + refPath);
                        }
                        break;
                }
            }
            else
            {
                refInfo = new ReferenceInfo(CurrentPath);
                _refs[value] = refInfo;
            }
            return null;
        }