public static void Deserialize(MemoryStream ms, JsonElement json) { var enumerate = json.EnumerateObject(); while (enumerate.MoveNext()) { var element = enumerate.Current; var parts = element.Name.ToString().Split(':'); var fieldNumber = Convert.ToInt64(parts[0]); var type = parts[parts.Length - 1]; var header = (fieldNumber << 3) | TagMap[type]; Varint.Write(ms, header); switch (type) { case "varint": Varint.Write(ms, element.Value.GetInt64()); break; case "int32": ms.Write(BitConverter.GetBytes(element.Value.GetInt32())); break; case "float32": ms.Write(BitConverter.GetBytes(element.Value.GetSingle())); break; case "int64": ms.Write(BitConverter.GetBytes(element.Value.GetInt64())); break; case "float64": ms.Write(BitConverter.GetBytes(element.Value.GetDouble())); break; case "string": string str = element.Value.GetString(); Varint.Write(ms, str.Length); ms.Write(Encoding.UTF8.GetBytes(str)); break; case "base64": { switch (element.Value.ValueKind) { case JsonValueKind.Object: { using var base64ms = new MemoryStream(); Deserialize(base64ms, element.Value); var buf = Encoding.UTF8.GetBytes(UrlBase64.Encode(base64ms.ToArray())); Varint.Write(ms, buf.Length); ms.Write(buf); break; } case JsonValueKind.String: string base64str = element.Value.GetString(); Varint.Write(ms, base64str.Length); ms.Write(Encoding.UTF8.GetBytes(base64str)); break; } break; } case "embedded": { using var embeddedMs = new MemoryStream(); Deserialize(embeddedMs, element.Value); Varint.Write(ms, embeddedMs.Length); embeddedMs.WriteTo(ms); break; } case "bytes": var buffer = element.Value.GetBytesFromBase64(); Varint.Write(ms, buffer.Length); ms.Write(buffer); break; default: throw new Exception("Unknown WireType"); } } }
public static void Deserialize(MemoryStream ms, IDictionary protobuf) { foreach (DictionaryEntry element in protobuf) { var parts = element.Key.ToString().Split(':'); var fieldNumber = Convert.ToInt64(parts[0]); var type = parts[parts.Length - 1]; var header = (fieldNumber << 3) | TagMap[type]; Varint.Write(ms, header); switch (type) { case "varint": Varint.Write(ms, Convert.ToInt64(element.Value)); break; case "int32": ms.Write(BitConverter.GetBytes((int)element.Value)); break; case "float32": ms.Write(BitConverter.GetBytes((float)element.Value)); break; case "int64": ms.Write(BitConverter.GetBytes((long)element.Value)); break; case "float64": ms.Write(BitConverter.GetBytes((double)element.Value)); break; case "string": string str = (string)element.Value; Varint.Write(ms, str.Length); ms.Write(Encoding.UTF8.GetBytes(str)); break; case "base64": { using var base64ms = new MemoryStream(); Deserialize(base64ms, (IDictionary)element.Value); base64ms.Position = 0; var base64Buffer = Encoding.UTF8.GetBytes(Convert.ToBase64String(base64ms.ToArray())); Varint.Write(ms, base64Buffer.Length); ms.Write(base64Buffer); break; } case "embedded": { using var embeddedMs = new MemoryStream(); Deserialize(embeddedMs, (IDictionary)element.Value); embeddedMs.Position = 0; Varint.Write(ms, (long)embeddedMs.Length); embeddedMs.CopyTo(ms); break; } case "bytes": var buffer = (byte[])element.Value; Varint.Write(ms, buffer.Length); ms.Write(buffer); break; default: throw new Exception("Unknown WireType"); } } }