1 module google.protobuf.empty; 2 3 import std.json : JSONValue; 4 import google.protobuf; 5 6 struct Empty 7 { 8 auto toProtobuf() 9 { 10 return cast(ubyte[]) null; 11 } 12 13 Empty fromProtobuf(R)(ref R inputRange) 14 { 15 import std.range : drop; 16 17 inputRange = inputRange.drop(inputRange.length); 18 return this; 19 } 20 21 JSONValue toJSONValue()() 22 { 23 return JSONValue(cast(JSONValue[string]) null); 24 } 25 26 Empty fromJSONValue()(JSONValue value) 27 { 28 import std.exception : enforce; 29 import std.json : JSON_TYPE; 30 import std.range : empty; 31 32 if (value.type == JSON_TYPE.NULL) 33 { 34 return this; 35 } 36 37 enforce!ProtobufException(value.type == JSON_TYPE.OBJECT && value.object.empty, 38 "Invalid google.protobuf.Empty JSON Encoding"); 39 40 return this; 41 } 42 } 43 44 unittest 45 { 46 import std.range : empty; 47 48 assert(Empty().toProtobuf.empty); 49 ubyte[] foo = [1, 2, 3]; 50 Empty().fromProtobuf(foo); 51 assert(foo.empty); 52 } 53 54 unittest 55 { 56 import std.json : JSON_TYPE; 57 import std.range : empty; 58 59 assert(Empty().toJSONValue.type == JSON_TYPE.OBJECT); 60 assert(Empty().toJSONValue.object.empty); 61 }