예제 #1
0
        private void ReferenceOrWriteSchema(JSchema context, JSchema schema, string propertyName, bool isDefinitions = false)
        {
            KnownSchema knownSchema = _knownSchemas.GetByJSchema(schema);

            if (knownSchema == null)
            {
#if DEBUG
                _writer.WriteValue("UNKNOWN SCHEMA: " + schema.ResolvedId);
#endif
                return;
            }

            if (propertyName != null)
            {
                _writer.WritePropertyName(propertyName);
            }

            if (ShouldWriteReference(knownSchema, isDefinitions))
            {
                Uri reference = ResolveReference(context, knownSchema);

                WriteReferenceObject(reference);
                return;
            }

            knownSchema.State = KnownSchemaState.Written;

            WriteSchemaInternal(schema);
        }
        private void ReferenceOrWriteSchema(JSchema context, JSchema schema, string propertyName, bool isDefinitions = false)
        {
            KnownSchema knownSchema = _knownSchemas.GetByJSchema(schema);

            if (knownSchema == null)
            {
                return;
            }

            if (propertyName != null)
            {
                _writer.WritePropertyName(propertyName);
            }

            if (ShouldWriteReference(knownSchema, isDefinitions))
            {
                KnownSchema currentKnownSchema = _knownSchemas.GetByJSchema(context);

                Uri reference;

                // Id is fully qualified
                // make it relative to the current schema
                if (currentKnownSchema.Id.IsAbsoluteUri)
                {
                    if (currentKnownSchema.Id.IsBaseOf(knownSchema.Id))
                    {
                        reference = currentKnownSchema.Id.MakeRelativeUri(knownSchema.Id);

                        // MakeRelativeUri escapes the result, need to unescape
                        reference = new Uri(Uri.UnescapeDataString(reference.OriginalString), UriKind.RelativeOrAbsolute);
                    }
                    else if (knownSchema.Id == currentKnownSchema.Id)
                    {
                        reference = new Uri("#", UriKind.RelativeOrAbsolute);
                    }
                    else
                    {
                        reference = knownSchema.Id;
                    }
                }
                else
                {
                    reference = knownSchema.Id;
                }

                WriteReferenceObject(reference);
                return;
            }

            knownSchema.State = KnownSchemaState.Written;

            WriteSchemaInternal(schema);
        }