예제 #1
0
        public TModel Deserialze <TModel>(Stream inputStream, Action <JsonSerializerSettings> settings) where TModel : class
        {
            // Read from stream directly without closing the stream
            StreamReader streamReader = new StreamReader(inputStream);

            using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) {
                // Setup JSON settings
                JsonSerializerSettings jsonSettings = JsonApi.CloneSettings(this._jsonSettings);
                settings?.Invoke(jsonSettings);

                // Deserialize
                return(JsonSerializer.CreateDefault(jsonSettings).Deserialize <TModel>(jsonReader));
            }
        }
예제 #2
0
        public void Serialize <TModel>(TModel model, Stream outputStream, Action <JsonSerializerSettings> settings, bool minify = false) where TModel : class
        {
            // Write to stream directly using the custom JSON writer without closing the stream
            TextWriter textWriter = new StreamWriter(outputStream);

            using (DescriptiveJsonWriter writer = new DescriptiveJsonWriter(textWriter)) {
                writer.Minify = minify;

                // Setup JSON Settings
                JsonSerializerSettings jsonSettings = JsonApi.CloneSettings(this._jsonSettings);
                settings?.Invoke(jsonSettings);

                // Serialize
                JsonSerializer.CreateDefault(jsonSettings).Serialize(writer, model);
                writer.Flush();
            }
        }