/// <summary> /// Writes properties of the model to JSON (asynchronous way). /// </summary> /// <param name="writer"></param> /// <param name="options"></param> /// <param name="ct">The cancellation token</param> /// <returns></returns> protected virtual async Task WriteModelPropsToJsonAsync(JsonWriter writer, BitOptions options, CancellationToken ct) { await writer.WritePropertyNameAsync("fver", ct).ConfigureAwait(false); await writer.WriteValueAsync(FormatVersionJson, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("mver", ct).ConfigureAwait(false); await writer.WriteValueAsync(ModelVersion, ct).ConfigureAwait(false); CheckModelId(); await writer.WritePropertyNameAsync("id", ct).ConfigureAwait(false); await writer.WriteValueAsync(Id, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("name", ct).ConfigureAwait(false); await writer.WriteValueAsync(Name, ct).ConfigureAwait(false); if (options.Contains(MetaDataReadWriteOptions.Description)) { await writer.WritePropertyNameAsync("desc", ct).ConfigureAwait(false); await writer.WriteValueAsync(Description, ct).ConfigureAwait(false); } if (options.Contains(MetaDataReadWriteOptions.CustomInfo)) { await writer.WritePropertyNameAsync("cstinf", ct).ConfigureAwait(false); await writer.WriteValueAsync(CustomInfo.ToString(), ct).ConfigureAwait(false); } }
/// <summary> /// Writes the content of the "CONST LIST" value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Different read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task</returns> protected override async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct) { await base.WritePropertiesToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("values", ct).ConfigureAwait(false); await writer.WriteStartArrayAsync(ct).ConfigureAwait(false); foreach (var value in Values) { await writer.WriteStartObjectAsync(ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("id", ct).ConfigureAwait(false); await writer.WriteValueAsync(value.Id, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("text", ct).ConfigureAwait(false); await writer.WriteValueAsync(value.Text, ct).ConfigureAwait(false); await writer.WriteEndObjectAsync(ct).ConfigureAwait(false); } await writer.WriteEndArrayAsync(ct).ConfigureAwait(false); }
/// <summary> /// Saves the model to a JSON file (asynchronous way). /// </summary> /// <param name="filePath"></param> /// <param name="options"></param> /// <returns>Task.</returns> public async Task LoadFromJsonFileAsync(string filePath, BitOptions options) { FilePath = filePath; using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { await LoadFromJsonStreamAsync(stream, options).ConfigureAwait(false); } }
/// <summary> /// Reads the content of the model from the specified JsonReader (asynchronous way). /// </summary> /// <param name="reader">The reader</param> /// <param name="options">Some read/write options.</param> /// <param name="ct">The cancellation token</param> /// <returns>Task.</returns> /// <exception cref="BadJsonFormatException"></exception> public async Task ReadFromJsonAsync(JsonReader reader, BitOptions options, CancellationToken ct = default) { if (!await reader.ReadAsync(ct).ConfigureAwait(false) || reader.TokenType != JsonToken.StartObject) { throw new BadJsonFormatException(reader.Path); } if (!options.Contains(MetaDataReadWriteOptions.KeepCurrent)) { Clear(); } while (await reader.ReadAsync(ct)) { if (reader.TokenType == JsonToken.PropertyName) { string propName = reader.Value.ToString(); await ReadOneModelPropFromJsonAsync(reader, propName, ct).ConfigureAwait(false); } else if (reader.TokenType == JsonToken.EndObject) { break; } } OnModelLoaded(); }
/// <summary> /// Loads the model from a string in JSON format (asynchronous way). /// </summary> /// <param name="json">A string in JSON format.</param> /// <param name="options">Different read/write options.</param> /// <returns>Task.</returns> public async Task LoadFromJsonStringAsync(string json, BitOptions options) { using (var textReader = new StringReader(json)) { using (var jsonReader = new JsonTextReader(textReader)) { await ReadFromJsonAsync(jsonReader, options).ConfigureAwait(false); } } }
/// <summary> /// Loads data model from JSON stream (asynchronous way). /// </summary> /// <param name="stream">A Stream object which contains data model definition.</param> /// <param name="options">Different read/write options. See <see cref="MetaDataReadWriterOptions"/> for details.</param> /// <return>Task</return> public async Task LoadFromJsonStreamAsync(Stream stream, BitOptions options) { using (var streamReader = new StreamReader(stream)) { using (var jsonReader = new JsonTextReader(streamReader)) { await ReadFromJsonAsync(jsonReader, options).ConfigureAwait(false); } } }
/// <summary> /// Saves the data model to a stream in JSON format (asynchronous way). /// </summary> /// <param name="stream">The stream to save the model to</param> /// <param name="options">Different read/write options</param> /// <returns></returns> public async Task SaveToJsonStreamAsync(Stream stream, BitOptions options) { using (var streamWriter = new StreamWriter(stream)) { using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter)) { await WriteToJsonAsync(jsonWriter, options).ConfigureAwait(false); } } }
/// <summary> /// Writes attribute's content to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer.</param> /// <returns>Task.</returns> /// <param name="options">Some read/write options</param> protected internal async Task WriteToJsonAsync(JsonWriter writer, BitOptions options) { await writer.WriteStartObjectAsync().ConfigureAwait(false); await WritePropertiesToJsonAsync(writer, options).ConfigureAwait(false); await writer.WriteEndObjectAsync().ConfigureAwait(false); }
/// <summary> /// Writes the content of the custom value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task</returns> protected override async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct) { await base.WritePropertiesToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("name", ct).ConfigureAwait(false); await writer.WriteValueAsync(ListName, ct).ConfigureAwait(false); }
/// <summary> /// Saves the data model to a file in JSON format (asynchronous way). /// </summary> /// <param name="filePath">The path to the result file</param> /// <param name="options">Different read/write options</param> /// <param name="ct">The cancellation token</param> /// <returns>Task</returns> public async Task SaveToJsonFileAsync(string filePath, BitOptions options, CancellationToken ct = default) { using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) { await SaveToJsonStreamAsync(stream, options, ct).ConfigureAwait(false); } }
/// <summary> /// Saves the model to a JSON file (asynchronous way). /// </summary> /// <param name="filePath"></param> /// <param name="options"></param> /// <param name="ct">The cancellation token</param> /// <returns>Task.</returns> public async Task LoadFromJsonFileAsync(string filePath, BitOptions options, CancellationToken ct = default) { FilePath = filePath; using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) { await LoadFromJsonStreamAsync(stream, options, ct).ConfigureAwait(false); } }
/// <summary> /// Writes the content of the text value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <returns>Task</returns> protected override async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions) { await base.WritePropertiesToJsonAsync(writer, rwOptions).ConfigureAwait(false); await writer.WritePropertyNameAsync("defval").ConfigureAwait(false); await writer.WriteValueAsync(defaultValue).ConfigureAwait(false); }
/// <summary> /// Loads the model from a string in JSON format (asynchronous way). /// </summary> /// <param name="json">A string in JSON format.</param> /// <param name="options">Different read/write options.</param> /// <param name="ct">The cancellation token</param> /// <returns>Task.</returns> public async Task LoadFromJsonStringAsync(string json, BitOptions options, CancellationToken ct = default) { using (var textReader = new StringReader(json)) { using (var jsonReader = new JsonTextReader(textReader)) { await ReadFromJsonAsync(jsonReader, options, ct).ConfigureAwait(false); } } }
/// <summary> /// Loads data model from JSON stream (asynchronous way). /// </summary> /// <param name="stream">A Stream object which contains data model definition.</param> /// <param name="options">Different read/write options. See <see cref="MetaDataReadWriterOptions"/> for details.</param> /// <param name="ct">The cancellation token</param> /// <return>Task</return> public async Task LoadFromJsonStreamAsync(Stream stream, BitOptions options, CancellationToken ct = default) { using (var streamReader = new StreamReader(stream)) { using (var jsonReader = new JsonTextReader(streamReader)) { await ReadFromJsonAsync(jsonReader, options, ct).ConfigureAwait(false); } } }
/// <summary> /// Saves the data model to a stream in JSON format (asynchronous way). /// </summary> /// <param name="stream">The stream to save the model to</param> /// <param name="options">Different read/write options</param> /// <param name="ct">The cancellation token</param> /// <returns></returns> public async Task SaveToJsonStreamAsync(Stream stream, BitOptions options, CancellationToken ct = default) { using (var streamWriter = new StreamWriter(stream)) { using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter)) { await WriteToJsonAsync(jsonWriter, options, ct).ConfigureAwait(false); } } }
/// <summary> /// Saves the data model to a file in JSON format (asynchronous way). /// </summary> /// <param name="filePath">The path to the result file</param> /// <param name="options">Different read/write options</param> /// <returns>Task</returns> public async Task SaveToJsonFileAsync(string filePath, BitOptions options) { using (var streamWriter = new StreamWriter(filePath)) { using (var jsonWriter = new JsonTextWriter(streamWriter)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; await WriteToJsonAsync(jsonWriter, options).ConfigureAwait(false); } } }
/// <summary> /// Writes the content of the data model to JSON using JsonWriter (asynchronous way). /// </summary> /// <param name="writer">An instance of JsonWriter class.</param> /// <param name="options">Read-write options</param> /// <param name="ct">The cancellation token</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions options, CancellationToken ct = default) { await writer.WriteStartObjectAsync(ct).ConfigureAwait(false); //root DataModel object await WriteModelPropsToJsonAsync(writer, options, ct).ConfigureAwait(false); await WriteContentToJsonAsync(writer, options, ct).ConfigureAwait(false); await writer.WriteEndObjectAsync(ct).ConfigureAwait(false); //close DataModel }
/// <summary> /// Writes the content of the data model to JSON using JsonWriter (asynchronous way). /// </summary> /// <param name="writer">An instance of JsonWriter class.</param> /// <param name="options">Read-write options</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions options) { await writer.WriteStartObjectAsync().ConfigureAwait(false); //root DataModel object await WriteModelPropsToJsonAsync(writer, options).ConfigureAwait(false); await WriteContentToJsonAsync(writer, options).ConfigureAwait(false); await writer.WriteEndObjectAsync().ConfigureAwait(false); //close DataModel }
/// <summary> /// Writes the content of the value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer.</param> /// <param name="rwOptions">Read/write options.</param> /// <returns>Task.</returns> protected virtual async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions) { await writer.WritePropertyNameAsync("id").ConfigureAwait(false); await writer.WriteValueAsync(Id).ConfigureAwait(false); await writer.WritePropertyNameAsync("rtype").ConfigureAwait(false); await writer.WriteValueAsync(ResultType).ConfigureAwait(false); }
/// <summary> /// Writes the list of entities to JSON (asynchronous way). /// </summary> /// <param name="writer">An instance of JsonWriter class.</param> /// <param name="rwOptions">Different read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct = default) { await writer.WriteStartArrayAsync(ct).ConfigureAwait(false); foreach (var ent in this) { await ent.WriteToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); } await writer.WriteEndArrayAsync(ct).ConfigureAwait(false); }
/// <summary> /// Writes the content of the value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer.</param> /// <param name="rwOptions">Read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task.</returns> protected virtual async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct) { await writer.WritePropertyNameAsync("id", ct).ConfigureAwait(false); await writer.WriteValueAsync(Id, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("rtype", ct).ConfigureAwait(false); await writer.WriteValueAsync(ResultType, ct).ConfigureAwait(false); }
/// <summary> /// Writes the list of entities to JSON (asynchronous way). /// </summary> /// <param name="writer">An instance of JsonWriter class.</param> /// <param name="rwOptions">Different read/write options.</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions rwOptions) { await writer.WriteStartArrayAsync().ConfigureAwait(false); foreach (var ent in this) { await ent.WriteToJsonAsync(writer, rwOptions).ConfigureAwait(false); } await writer.WriteEndArrayAsync().ConfigureAwait(false); }
/// <summary> /// Saves the model to a string in JSON format (asynchronous way). /// </summary> /// <param name="options">Different read/write options.</param> /// <param name="ct">The cancellation token</param> /// <returns>Task<System.String>.</returns> public async Task <string> SaveToJsonStringAsync(BitOptions options, CancellationToken ct = default) { var result = new StringBuilder(1000); using (var textWriter = new StringWriter(result)) { using (var jsonWriter = new JsonTextWriter(textWriter)) { await WriteToJsonAsync(jsonWriter, options, ct).ConfigureAwait(false); } } return(result.ToString()); }
/// <summary> /// Writes the content of the "CUSTOM" value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <returns>Task</returns> protected override async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions) { await base.WritePropertiesToJsonAsync(writer, rwOptions).ConfigureAwait(false); await writer.WritePropertyNameAsync("data").ConfigureAwait(false); await writer.WriteValueAsync(Data).ConfigureAwait(false); await writer.WritePropertyNameAsync("type").ConfigureAwait(false); await writer.WriteValueAsync(_tag).ConfigureAwait(false); }
/// <summary> /// Writes the value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct = default) { await writer.WriteStartObjectAsync(ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("tag", ct).ConfigureAwait(false); await writer.WriteValueAsync(Tag, ct).ConfigureAwait(false); await WritePropertiesToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); await writer.WriteEndObjectAsync(ct).ConfigureAwait(false); }
/// <summary> /// Writes the value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <returns>Task</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions rwOptions) { await writer.WriteStartObjectAsync().ConfigureAwait(false); await writer.WritePropertyNameAsync("tag").ConfigureAwait(false); await writer.WriteValueAsync(Tag).ConfigureAwait(false); await WritePropertiesToJsonAsync(writer, rwOptions).ConfigureAwait(false); await writer.WriteEndObjectAsync().ConfigureAwait(false); }
/// <summary> /// Writes the list of the value editors to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer.</param> /// <param name="rwOptions">Read/write options.</param> /// <param name="includeDefaults">if set to <c>true</c> then the default editors must be saved as well.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions rwOptions, bool includeDefaults = false, CancellationToken ct = default) { await writer.WriteStartArrayAsync(ct).ConfigureAwait(false); foreach (ValueEditor editor in this) { if (editor != null && (includeDefaults || !editor.IsDefault)) { await editor.WriteToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); } } await writer.WriteEndArrayAsync(ct).ConfigureAwait(false); }
/// <summary> /// Writes the content of the entity to JSON (asynchronious way) /// </summary> /// <param name="writer">The writer.</param> /// <param name="options">Some read/write options.</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions options) { await writer.WriteStartObjectAsync().ConfigureAwait(false); await WritePropertiesToJsonAsync(writer).ConfigureAwait(false); await writer.WritePropertyNameAsync("attrs").ConfigureAwait(false); await Attributes.WriteToJsonAsync(writer, options).ConfigureAwait(false); if (SubEntities.Count > 0) { await writer.WritePropertyNameAsync("ents").ConfigureAwait(false); await SubEntities.WriteToJsonAsync(writer, options).ConfigureAwait(false); } await writer.WriteEndObjectAsync().ConfigureAwait(false); }
/// <summary> /// Writes the content of the entity to JSON (asynchronious way) /// </summary> /// <param name="writer">The writer.</param> /// <param name="options">Some read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task.</returns> public async Task WriteToJsonAsync(JsonWriter writer, BitOptions options, CancellationToken ct = default) { await writer.WriteStartObjectAsync(ct).ConfigureAwait(false); await WritePropertiesToJsonAsync(writer, ct).ConfigureAwait(false); await writer.WritePropertyNameAsync("attrs", ct).ConfigureAwait(false); await Attributes.WriteToJsonAsync(writer, options, ct).ConfigureAwait(false); if (SubEntities.Count > 0) { await writer.WritePropertyNameAsync("ents", ct).ConfigureAwait(false); await SubEntities.WriteToJsonAsync(writer, options, ct).ConfigureAwait(false); } await writer.WriteEndObjectAsync(ct).ConfigureAwait(false); }
/// <summary> /// Writes the content of the custom value editor to JSON (asynchronous way). /// </summary> /// <param name="writer">The writer</param> /// <param name="rwOptions">Read/write options.</param> /// <param name="ct">The cancellation token.</param> /// <returns>Task</returns> protected override async Task WritePropertiesToJsonAsync(JsonWriter writer, BitOptions rwOptions, CancellationToken ct) { await base.WritePropertiesToJsonAsync(writer, rwOptions, ct).ConfigureAwait(false); if (ExtraParams.Count > 0) { await writer.WritePropertyNameAsync("extraParams", ct).ConfigureAwait(false); await writer.WriteStartObjectAsync(ct).ConfigureAwait(false); foreach (var kv in ExtraParams) { await writer.WritePropertyNameAsync(kv.Key, ct).ConfigureAwait(false); await writer.WriteValueAsync(kv.Value, ct).ConfigureAwait(false); } await writer.WriteEndObjectAsync(ct).ConfigureAwait(false); } }