internal static DeleteData ParseDeleteData(byte[] json) { var r = JReader.FromUtf8Bytes(json); try { DataKind kind = null; string key = null; int version = 0; for (var obj = r.Object().WithRequiredProperties(_deleteRequiredProperties); obj.Next(ref r);) { if (obj.Name == "path") { TryParsePath(r.String(), out kind, out key); } else if (obj.Name == "version") { version = r.Int(); } } return(new DeleteData(kind, key, version)); } catch (Exception e) { throw r.TranslateException(e); } }
internal static PutData ParsePutData(byte[] json) { var r = JReader.FromUtf8Bytes(json); try { string path = null; FullDataSet <ItemDescriptor> data = new FullDataSet <ItemDescriptor>(); for (var obj = r.Object().WithRequiredProperties(_putRequiredProperties); obj.Next(ref r);) { if (obj.Name == "path") { path = r.String(); } else if (obj.Name == "data") { data = ParseFullDataset(ref r); } } return(new PutData(path, data)); } catch (Exception e) { throw r.TranslateException(e); } }
internal static PatchData ParsePatchData(byte[] json) { var r = JReader.FromUtf8Bytes(json); try { DataKind kind = null; string key = null; for (var obj = r.Object().WithRequiredProperties(_patchRequiredProperties); obj.Next(ref r);) { if (obj.Name == "path") { TryParsePath(r.String(), out kind, out key); if (kind is null) { // An unrecognized path isn't considered an error; we'll just return a null kind, // indicating that we should ignore this event. return(new PatchData(null, null, new ItemDescriptor())); } } else if (obj.Name == "data") { if (kind != null) { // If kind is null here, it means we happened to read the "data" property before // the "path" property, so we don't yet know what kind of data model object this // is, so we can't parse it yet and we'll have to do a second pass. var item = kind.DeserializeFromJReader(ref r); return(new PatchData(kind, key, item)); } } } // If we got here, it means we couldn't parse the data model object yet because we saw the // "data" property first. But we definitely saw both properties (otherwise we would've got // an error due to using WithRequiredProperties) so kind is now non-null. var r1 = JReader.FromUtf8Bytes(json); for (var obj = r1.Object(); obj.Next(ref r1);) { if (obj.Name == "data") { return(new PatchData(kind, key, kind.DeserializeFromJReader(ref r1))); } } throw new RequiredPropertyException("data", json.Length); } catch (Exception e) { throw r.TranslateException(e); } }