private void ReferenceOrWriteSchema(JSchema context, JSchema schema, string propertyName)
        {
            KnownSchema knownSchema = _knownSchemas.SingleOrDefault(s => s.Schema == schema);

            if (knownSchema == null)
            {
                return;
            }

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

            if (ShouldWriteReference(knownSchema))
            {
                KnownSchema currentKnownSchema = _knownSchemas.Single(s => s.Schema == 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.InlineWritten;

            WriteSchemaInternal(schema);
        }
示例#2
0
        public void WriteSchema(JSchema schema)
        {
            ValidationUtils.ArgumentNotNull(schema, nameof(schema));

            _rootSchema = schema;

            _knownSchemas.Clear();

            JSchemaDiscovery discovery;

            if (_externalSchemas != null)
            {
                foreach (ExternalSchema externalSchema in _externalSchemas)
                {
                    discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.External);
                    discovery.Discover(externalSchema.Schema, externalSchema.Uri);
                }
            }

            if (_version == SchemaVersion.Unset)
            {
                _version = SchemaVersionHelpers.MapSchemaUri(schema.SchemaVersion);
            }

            discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.InlinePending);
            discovery.Discover(schema, null);

            KnownSchema rootKnownSchema = _knownSchemas.Single(s => s.Schema == schema);

            rootKnownSchema.State = KnownSchemaState.Written;

            WriteSchemaInternal(schema);
        }