1 module vision.json.patch.operation.basic; 2 3 public import vision.json.patch.commons; 4 public import vision.json.pointer; 5 6 /** 7 * Basic class for all diff operations 8 */ 9 class DiffOperation 10 { 11 import std.typecons: Tuple; 12 import std.range: InputRange; 13 14 alias DataRange = InputRange!(Tuple!(string, const JsonItem)); 15 16 /// Error message for last failed operation 17 static string lastError; 18 /// Path of element to affect 19 const JsonPointer path; 20 /// Operation name 21 abstract @property string op() pure const @safe; 22 23 /// Apply this operation to document 24 bool applyTo(ref JsonItem document) const 25 { 26 return applyToPtr(&document); 27 } 28 29 /// Apply this operation to document by pointer 30 abstract bool applyToPtr(JsonItem* document) const; 31 32 this(const string path) @safe 33 { 34 this.path = JsonPointer(path); 35 } 36 37 this(const JsonPointer path) @safe 38 { 39 this.path = path; 40 } 41 42 /// Convert to Json element 43 JsonItem toJson() const 44 { 45 return JsonItem(["op": JsonItem(op), "path": JsonItem(path.toString)]); 46 } 47 48 string toJsonString() const 49 { 50 import std.json : toJSON, JSONOptions; 51 52 auto json = toJson; 53 return json.toJSON(false, JSONOptions.doNotEscapeSlashes); 54 } 55 56 /// Return error and store error message 57 static bool error(string errorMessage) 58 { 59 lastError = errorMessage; 60 return false; 61 } 62 }