public override void Write(Utf8JsonWriter writer, FragmentDistributionDeliveredEvent value, JsonSerializerOptions options) { writer.WriteStartObject(); writer.WriteString(nameof(FragmentDistributionDeliveredEvent.EventId), value.EventId); writer.WriteString(nameof(FragmentDistributionDeliveredEvent.Hash), value.Hash); writer.WritePropertyName(nameof(FragmentDistributionDeliveredEvent.Receivers)); JsonSerializer.Serialize(writer, value.Receivers, options); writer.WriteBase64String(nameof(FragmentDistributionDeliveredEvent.Data), value.Data); writer.WriteEndObject(); }
public override void Write(Utf8JsonWriter writer, KVPair value, JsonSerializerOptions options) { writer.WriteStartObject(); writer.WriteString("Key", value.Key); writer.WriteNumber("CreateIndex", value.CreateIndex); writer.WriteNumber("ModifyIndex", value.ModifyIndex); writer.WriteNumber("LockIndex", value.LockIndex); writer.WriteNumber("Flags", value.Flags); writer.WriteBase64String("Session", value.Value); writer.WriteString("Session", value.Session); writer.WriteEndObject(); }
public override void Write(Utf8JsonWriter writer, BitArray value, JsonSerializerOptions options) { byte[] ret = new byte[(value.Length - 1) / 8 + 1]; value.CopyTo(ret, 0); writer.WriteStartObject(); writer.WriteNumber("Length", value.Length); writer.WriteBase64String("Value", ret); writer.WriteEndObject(); }
public static DataTable ToJson(this DataTable dt, out JsonDocument json) { var outputBuffer = new ArrayBufferWriter <byte>(); using (var jsonWriter = new Utf8JsonWriter(outputBuffer)) { jsonWriter.WriteStartArray(); foreach (DataRow dr in dt.Rows) { jsonWriter.WriteStartObject(); foreach (DataColumn dc in dt.Columns) { object val = dr[dc]; if (val != DBNull.Value && val != null) { if (val is string) { jsonWriter.WriteString(dc.ColumnName, (string)val); } if (val is bool) { jsonWriter.WriteBoolean(dc.ColumnName, (bool)val); } if (val is sbyte || val is byte || val is short || val is ushort || val is int || val is uint || val is long || val is ulong || val is float || val is double || val is decimal) { jsonWriter.WriteNumber(dc.ColumnName, (decimal)val); } if (val is byte[]) { jsonWriter.WriteBase64String(dc.ColumnName, (byte[])val); } } } jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); } json = JsonDocument.Parse(Encoding.UTF8.GetString(outputBuffer.WrittenSpan)); return(dt); }
void IJsonSerializable.WriteProperties(Utf8JsonWriter json) { if (Certificate != null) { if (Policy != null && Policy.ContentType == CertificateContentType.Pem) { string value = Encoding.ASCII.GetString(Certificate); json.WriteString(s_valuePropertyNameBytes, value); } else { json.WriteBase64String(s_valuePropertyNameBytes, Certificate); } } if (!string.IsNullOrEmpty(Password)) { json.WriteString(s_passwordPropertyNameBytes, Password); } if (Policy != null) { json.WriteStartObject(s_policyPropertyNameBytes); ((IJsonSerializable)Policy).WriteProperties(json); json.WriteEndObject(); } if (Enabled.HasValue) { json.WriteStartObject(s_attributesPropertyNameBytes); json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); json.WriteEndObject(); } if (!_tags.IsNullOrEmpty()) { json.WriteStartObject(s_tagsPropertyNameBytes); foreach (KeyValuePair <string, string> kvp in _tags) { json.WriteString(kvp.Key, kvp.Value); } json.WriteEndObject(); } }
internal async ValueTask <string> SerializeAsync() { using var stream = new MemoryStream(); using Utf8JsonWriter writer = new Utf8JsonWriter(stream); writer.WriteStartObject(); writer.WriteString("dueTime", ConverterUtils.ConvertTimeSpanValueInDaprFormat(this.DueTime)); writer.WriteString("period", ConverterUtils.ConvertTimeSpanValueInDaprFormat(this.Period)); writer.WriteBase64String("data", this.Data); writer.WriteEndObject(); await writer.FlushAsync(); return(Encoding.UTF8.GetString(stream.ToArray())); }
void IJsonSerializable.WriteProperties(Utf8JsonWriter json) { if (Value != null) { json.WriteBase64String(s_valuePropertyNameBytes, Value); } if (!string.IsNullOrEmpty(Password)) { json.WriteString(s_passwordPropertyNameBytes, Password); } if (Policy != null) { json.WriteStartObject(s_policyPropertyNameBytes); ((IJsonSerializable)Policy).WriteProperties(json); json.WriteEndObject(); } if (Enabled.HasValue) { json.WriteStartObject(s_attributesPropertyNameBytes); json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); json.WriteEndObject(); } if (Tags != null) { json.WriteStartObject(s_tagsPropertyNameBytes); foreach (KeyValuePair <string, string> kvp in Tags) { json.WriteString(kvp.Key, kvp.Value); } json.WriteEndObject(); } }
public static void WriteJsonusingUTF8Writer(string path) { var writeOptions = new JsonWriterOptions { Indented = true, SkipValidation = false }; var bufferwriter = new ArrayBufferWriter <byte>(); using (Utf8JsonWriter writer = new Utf8JsonWriter(bufferwriter, writeOptions)) { writer.WriteStartObject(); writer.WriteNumber("ID", 1); writer.WriteNull("ID2"); writer.WriteBoolean("IsValid", true); writer.WriteString("CreatedDate", DateTimeOffset.UtcNow); writer.WriteNumber("NumValue", 1.2345); //Example of a comment writer.WriteCommentValue("Example of a comment"); writer.WriteStartArray("NumArray"); writer.WriteNumberValue(1.23); writer.WriteNumberValue(4.56); writer.WriteCommentValue("Example of a comment"); writer.WriteNumberValue(7.89); writer.WriteEndArray(); writer.WriteStartObject("Object"); writer.WriteNumber("ID", 1); writer.WriteString("Name", "InnerObject"); writer.WriteBase64String("FileData", File.ReadAllBytes("StaticFiles/SampleText.txt")); writer.WriteEndObject(); writer.WriteEndObject(); } string json = Encoding.UTF8.GetString(bufferwriter.WrittenSpan); Console.WriteLine(json); File.WriteAllBytes(path, bufferwriter.WrittenSpan.ToArray()); }
public override void Write(Utf8JsonWriter writer, SecureString?value, JsonSerializerOptions options) { if (value == null) { throw new ArgumentNullException(nameof(value)); } IntPtr pointer = IntPtr.Zero; try { pointer = Marshal.SecureStringToGlobalAllocUnicode(value); writer.WriteStartObject(); writer.WriteBoolean(EncryptionPropertyName, true); writer.WriteBase64String(ValuePropertyName, ProtectedData.Protect(Encoding.Unicode.GetBytes(Marshal.PtrToStringUni(pointer) ?? string.Empty), Entropy, DataProtectionScope)); writer.WriteEndObject(); } finally { Marshal.ZeroFreeGlobalAllocUnicode(pointer); } }
public override void Write(Utf8JsonWriter writer, CollectionsOfPrimitives value, JsonSerializerOptions options) { if (value == null) { writer.WriteNullValue(); return; } writer.WriteStartObject(); writer.WriteBase64String(ByteArrayText, value.ByteArray); writer.WritePropertyName(DateTimeArrayText); JsonConverterForDateTimeArray.Instance.Write(writer, value.DateTimeArray, options); writer.WritePropertyName(DictionaryText); JsonConverterForDictionaryStringString.Instance.Write(writer, value.Dictionary, options); writer.WritePropertyName(ListOfIntText); JsonConverterForListInt32.Instance.Write(writer, value.ListOfInt, options); writer.WriteEndObject(); }
public async Task <bool> UploadFileAsync(Stream fileToUpload, int slot, string name, CancellationToken cancellationToken = default) { if (fileToUpload == null) { throw new ArgumentNullException(nameof(fileToUpload)); } using var postStream = new MemoryStream(); { using var writer = new Utf8JsonWriter(postStream); writer.WriteStartObject(); var nowTime = DateTime.UtcNow.Ticks; // Fix this to be unix epoch milliseconds (js getDate) writer.WriteString("m", "start_write_program"); writer.WriteStartObject("p"); writer.WriteNumber("slotid", slot); writer.WriteNumber("size", fileToUpload.Length); writer.WriteStartObject("meta"); writer.WriteNumber("created", nowTime); writer.WriteNumber("modified", nowTime); writer.WriteBase64String("name", Encoding.UTF8.GetBytes(name)); writer.WriteString("type", "python"); writer.WriteString("project_id", "HelloWorld12"); writer.WriteEndObject(); writer.WriteEndObject(); string randomString = "1abc"; writer.WriteString("i", randomString); writer.WriteEndObject(); } postStream.WriteByte(13); postStream.Seek(0, SeekOrigin.Begin); await postStream.CopyToAsync(spikeConnection.WriteStream, cancellationToken); await spikeConnection.WriteStream.FlushAsync(cancellationToken); var readValue = await programWriteChannel.Reader.ReadAsync(cancellationToken); var startResponse = (StartWriteProgramResponse)readValue; Memory <byte> memoryBuffer = new byte[startResponse.BlockSize]; do { var readBytes = await fileToUpload.ReadAsync(memoryBuffer, cancellationToken); postStream.Seek(0, SeekOrigin.Begin); postStream.SetLength(0); { using var writer = new Utf8JsonWriter(postStream); writer.WriteStartObject(); writer.WriteString("m", "write_package"); writer.WriteStartObject("p"); writer.WriteBase64String("data", memoryBuffer.Span.Slice(0, readBytes)); writer.WriteString("transferid", startResponse.TransferId); writer.WriteEndObject(); string randomString = "2abc"; writer.WriteString("i", randomString); writer.WriteEndObject(); } postStream.WriteByte(13); postStream.Seek(0, SeekOrigin.Begin); await postStream.CopyToAsync(spikeConnection.WriteStream, cancellationToken); await spikeConnection.WriteStream.FlushAsync(cancellationToken); var readWriteResponse = await programWriteChannel.Reader.ReadAsync(cancellationToken); ; }while (fileToUpload.Position < fileToUpload.Length); postStream.Seek(0, SeekOrigin.Begin); postStream.SetLength(0); return(true); }
public void Write(IEnumerable <IRow> rows) { var options = new JsonWriterOptions() { Indented = _context.Connection.Format == "json" }; var jw = new Utf8JsonWriter(_stream, options); jw.WriteStartArray(); foreach (var row in rows) { jw.WriteStartObject(); for (int i = 0; i < _fields.Length; i++) { if (_formats[i] == string.Empty) { switch (_fields[i].Type) { case "bool": case "boolean": jw.WriteBoolean(_fields[i].Alias, (bool)row[_fields[i]]); break; case "byte": jw.WriteNumber(_fields[i].Alias, System.Convert.ToUInt32(row[_fields[i]])); break; case "byte[]": jw.WriteBase64String(_fields[i].Alias, (byte[])row[_fields[i]]); break; case "date": case "datetime": jw.WriteString(_fields[i].Alias, (System.DateTime)row[_fields[i]]); break; case "decimal": jw.WriteNumber(_fields[i].Alias, (decimal)row[_fields[i]]); break; case "double": jw.WriteNumber(_fields[i].Alias, (double)row[_fields[i]]); break; case "float": case "real": case "single": jw.WriteNumber(_fields[i].Alias, (float)row[_fields[i]]); break; case "guid": jw.WriteString(_fields[i].Alias, (System.Guid)row[_fields[i]]); break; case "int": case "int32": jw.WriteNumber(_fields[i].Alias, (int)row[_fields[i]]); break; case "int64": case "long": jw.WriteNumber(_fields[i].Alias, (long)row[_fields[i]]); break; case "int16": case "short": jw.WriteNumber(_fields[i].Alias, (short)row[_fields[i]]); break; case "string": jw.WriteString(_fields[i].Alias, (string)row[_fields[i]]); break; case "uint": case "uint32": jw.WriteNumber(_fields[i].Alias, (uint)row[_fields[i]]); break; case "uint16": case "ushort": jw.WriteNumber(_fields[i].Alias, (ushort)row[_fields[i]]); break; case "uint64": case "ulong": jw.WriteNumber(_fields[i].Alias, (ulong)row[_fields[i]]); break; default: // char, object jw.WriteString(_fields[i].Alias, row[_fields[i]].ToString()); break; } } else { jw.WriteString(_fields[i].Alias, string.Format(_formats[i], row[_fields[i]])); } } jw.WriteEndObject(); _context.Entity.Inserts++; jw.FlushAsync().ConfigureAwait(false); } jw.WriteEndArray(); jw.FlushAsync().ConfigureAwait(false); }
private async Task DoStateChangesTransactionallyAsync(string actorType, string actorId, IReadOnlyCollection <ActorStateChange> stateChanges, CancellationToken cancellationToken = default) { // Transactional state update request body: /* * [ * { * "operation": "upsert", * "request": { * "key": "key1", * "value": "myData" * } * }, * { * "operation": "delete", * "request": { * "key": "key2" * } * } * ] */ using var stream = new MemoryStream(); using var writer = new Utf8JsonWriter(stream); writer.WriteStartArray(); foreach (var stateChange in stateChanges) { writer.WriteStartObject(); var operation = this.GetDaprStateOperation(stateChange.ChangeKind); writer.WriteString("operation", operation); // write the requestProperty writer.WritePropertyName("request"); writer.WriteStartObject(); // start object for request property switch (stateChange.ChangeKind) { case StateChangeKind.Remove: writer.WriteString("key", stateChange.StateName); break; case StateChangeKind.Add: case StateChangeKind.Update: writer.WriteString("key", stateChange.StateName); // perform default json serialization if custom serializer was not provided. if (this.actorStateSerializer != null) { var buffer = this.actorStateSerializer.Serialize(stateChange.Type, stateChange.Value); writer.WriteBase64String("value", buffer); } else { writer.WritePropertyName("value"); JsonSerializer.Serialize(writer, stateChange.Value, stateChange.Type); } break; default: break; } writer.WriteEndObject(); // end object for request property writer.WriteEndObject(); } writer.WriteEndArray(); await writer.FlushAsync(); var content = Encoding.UTF8.GetString(stream.ToArray()); await ActorRuntime.DaprInteractor.SaveStateTransactionallyAsync(actorType, actorId, content, cancellationToken); }
public void WriteDdbBinary(byte[] value) { JsonWriter.WriteStartObject(); JsonWriter.WriteBase64String(DdbTypeNames.Binary, value); JsonWriter.WriteEndObject(); }
public override void Write(JsonEncodedText propertyName, byte[] value, Utf8JsonWriter writer) { writer.WriteBase64String(propertyName, value); }
// Too slow currently; AOT/.NET 5 should help // Filesize: 50_274_091 // // writer.WriteStringValue(Convert.ToBase64String(value._data)); // Duration: 20:59 -> 24:04 // // writer.WriteStartArray(); // for (var i = 0; i < value._data.Length; i++) writer.WriteNumberValue(value._data[i]); // writer.WriteEndArray(); // Duration: 41:44 -> 49:43 public override void Write(Utf8JsonWriter writer, ByteArray value, JsonSerializerOptions options) => writer.WriteBase64String("Data", value.Data);
private static void WriteField(Utf8JsonWriter writer, Field field, IRow row, string alias = null) { var name = alias ?? (field.Label == string.Empty ? field.Alias : field.Label); switch (field.Type) { case "bool": case "boolean": writer.WriteBoolean(name, (bool)row[field]); break; case "byte": writer.WriteNumber(name, System.Convert.ToUInt32(row[field])); break; case "byte[]": writer.WriteBase64String(name, (byte[])row[field]); break; case "date": case "datetime": writer.WriteString(name, (System.DateTime)row[field]); break; case "decimal": writer.WriteNumber(name, (decimal)row[field]); break; case "double": writer.WriteNumber(name, (double)row[field]); break; case "float": case "real": case "single": writer.WriteNumber(name, (float)row[field]); break; case "guid": writer.WriteString(name, (System.Guid)row[field]); break; case "int": case "int32": writer.WriteNumber(name, (int)row[field]); break; case "int64": case "long": writer.WriteNumber(name, (long)row[field]); break; case "int16": case "short": writer.WriteNumber(name, (short)row[field]); break; case "string": writer.WriteString(name, (string)row[field]); break; case "uint": case "uint32": writer.WriteNumber(name, (uint)row[field]); break; case "uint16": case "ushort": writer.WriteNumber(name, (ushort)row[field]); break; case "uint64": case "ulong": writer.WriteNumber(name, (ulong)row[field]); break; default: // char, object writer.WriteString(name, row[field].ToString()); break; } }
private async Task WriteValueAsync(int i) { var info = m_DbColumns.Span[i]; if (await m_SqlDataReader.IsDBNullAsync(i)) { m_JsonWriter.WriteNull(info.Name); } else { switch (info.Type) { case "int": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <int>(i)); break; case "bigint": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <long>(i)); break; case "smallint": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <short>(i)); break; case "tinyint": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte>(i)); break; case "bit": m_JsonWriter.WriteBoolean(info.Name, await m_SqlDataReader.GetFieldValueAsync <bool>(i)); break; case "numeric": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <decimal>(i)); break; case "decimal": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <decimal>(i)); break; case "smallmoney": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <decimal>(i)); break; case "money": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <decimal>(i)); break; case "float": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <double>(i)); break; case "real": m_JsonWriter.WriteNumber(info.Name, await m_SqlDataReader.GetFieldValueAsync <float>(i)); break; case "date": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <DateTime>(i)); break; case "datetime": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <DateTime>(i)); break; case "datetime2": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <DateTime>(i)); break; case "smalldatetime": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <DateTime>(i)); break; case "datetimeoffset": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <DateTimeOffset>(i)); break; case "time": m_JsonWriter.WriteString(info.Name, (await m_SqlDataReader.GetFieldValueAsync <TimeSpan>(i)).ToString()); break; case "binary": m_JsonWriter.WriteBase64String(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte[]>(i)); break; case "varbinary": m_JsonWriter.WriteBase64String(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte[]>(i)); break; case "image": m_JsonWriter.WriteBase64String(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte[]>(i)); break; case "rowversion": m_JsonWriter.WriteBase64String(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte[]>(i)); break; case "timestamp": m_JsonWriter.WriteBase64String(info.Name, await m_SqlDataReader.GetFieldValueAsync <byte[]>(i)); break; case "uniqueidentifier": m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <Guid>(i)); break; default: m_JsonWriter.WriteString(info.Name, await m_SqlDataReader.GetFieldValueAsync <string>(i)); break; } } }
public void WriteBase64String(string key, byte[] arry) { writer.WriteBase64String(key, new ReadOnlySpan <byte>(arry)); }
private static void WriteSingleRow(System.Data.IDataRecord rdr, string[] fieldNamesToUse, Type[] types, Utf8JsonWriter jsonWriter) { jsonWriter.WriteStartObject(); for (int p = 0; p < fieldNamesToUse.Length; p++) { if (rdr.IsDBNull(p)) { jsonWriter.WriteNull(fieldNamesToUse[p]); } else if (types[p] == typeof(string)) { jsonWriter.WriteString(fieldNamesToUse[p], rdr.GetString(p)); } else if (types[p] == typeof(DateTime)) { jsonWriter.WriteString(fieldNamesToUse[p], rdr.GetDateTime(p)); } else if (types[p] == typeof(DateTimeOffset)) { jsonWriter.WriteString(fieldNamesToUse[p], (DateTimeOffset)rdr.GetValue(p)); } else if (types[p] == typeof(bool)) { jsonWriter.WriteBoolean(fieldNamesToUse[p], rdr.GetBoolean(p)); } else if (types[p] == typeof(Guid)) { jsonWriter.WriteString(fieldNamesToUse[p], rdr.GetGuid(p)); } else if (types[p] == typeof(short)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetInt16(p)); } else if (types[p] == typeof(int)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetInt32(p)); } else if (types[p] == typeof(long)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetInt64(p)); } else if (types[p] == typeof(float)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetFloat(p)); } else if (types[p] == typeof(double)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetDouble(p)); } else if (types[p] == typeof(decimal)) { jsonWriter.WriteNumber(fieldNamesToUse[p], rdr.GetDecimal(p)); } else if (types[p] == typeof(byte[])) { jsonWriter.WriteBase64String(fieldNamesToUse[p], (byte[])rdr.GetValue(p)); } else { string?vl = rdr.GetValue(p)?.ToString(); jsonWriter.WriteString(fieldNamesToUse[p], vl); } } jsonWriter.WriteEndObject(); }
public override void Write(Utf8JsonWriter writer, byte[] value, JsonEncodedText propertyName, JsonSerializerOptions options) { writer.WriteBase64String(propertyName, value); }
public void Write(Utf8JsonWriter writer) { writer.WriteStartObject(); writer.WriteBase64String(DdbTypeNames.Binary, _value); writer.WriteEndObject(); }