<commit_before>﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema;
using NJsonSchema.Generation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Threax.AspNetCore.Halcyon.Ext
{
    public class SchemaJsonConverter : JsonConverter
    {
        private static readonly TypeInfo JsonSchema4TypeInfo = typeof(JsonSchema4).GetTypeInfo();

        public override bool CanConvert(Type objectType)
        {
            return JsonSchema4TypeInfo.IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException("Reading json schemas is not supported");
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var jsonSchema = value as JsonSchema4;
            //This single line will do the same thing, but the object is not correct, the actual code for this
            //function does what ToJson below does, but uses json.net to serialize the schema so the settings
            //match the rest of the system.
            //writer.WriteRawValue(jsonSchema.ToJson(HalcyonConvention.DefaultJsonSchemaGeneratorSettings));
            JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(jsonSchema);
            using (var sw = new StringWriter())
            {
                serializer.Serialize(sw, jsonSchema);
                var json = JsonSchemaReferenceUtilities.ConvertPropertyReferences(sw.ToString());
                writer.WriteRawValue(json);
            }
        }<commit_msg>Changed the way json schemas are serialized, they appear to work correctly now.<commit_after>﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema;
using NJsonSchema.Generation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Threax.AspNetCore.Halcyon.Ext
{
    public class SchemaJsonConverter : JsonConverter
    {
        private static readonly TypeInfo JsonSchema4TypeInfo = typeof(JsonSchema4).GetTypeInfo();

        public override bool CanConvert(Type objectType)
        {
            return JsonSchema4TypeInfo.IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException("Reading json schemas is not supported");
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var jsonSchema = value as JsonSchema4;
            //Going with this line to generate schema json for now. In the past there were problems with this
            //not generating the correct json, which are discussed below. For now the following seems to be working.
            //It will make all the property names lowercase except the definitions, which will stay in their original
            //case, which is what we need, or else the definitions cannot be resolved.
            writer.WriteRawValue(jsonSchema.ToJson());

            //-------------------------------------------------------------------------------
            //This is the same as the line above since in the actual NJsonSchema source code the settings passed in are ignored.
            //At least this was true as of 8-23-2017. If in the future you have problems, try using this version first
            //to see if the json schema generator settings are honored.
            //writer.WriteRawValue(jsonSchema.ToJson(HalcyonConvention.DefaultJsonSchemaGeneratorSettings));
            //-------------------------------------------------------------------------------

            //-------------------------------------------------------------------------------
            //This code will use the current json serializer settings, but will mess up the definitions by lower casing the property names there.
            //No way could be found to avoid this before it was discovered that ToJson appears to be correct, but leaving this in as reference
            //for when this inevitably breaks in the future.
            //JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(jsonSchema);
            //using (var sw = new StringWriter())
            //{
            //    serializer.Serialize(sw, jsonSchema);
            //    var json = JsonSchemaReferenceUtilities.ConvertPropertyReferences(sw.ToString());
            //    writer.WriteRawValue(json);
            //}
            //-------------------------------------------------------------------------------
        }
{'fraction_non_alphanumeric': 0.12355212355212356, 'fraction_numerical': 0.003048160942897785, 'mean_word_length': 2.165273311897106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '528104', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1061, 'n_words': 399}