public object Interpret(BaseSchema schema, object value, IProcessContext context, string path) { if (value == null || !CanInterpret(schema)) { return(value); } var uid = JsonPointer.GetChild(value, "uid") as string; if (uid == null) { // schema validation threw error when uid is required, so here when uid is null, it must be optional, which is allowed return(value); } if (string.IsNullOrEmpty(uid)) { Logger.LogWarning($"Invalid xrefProperties for /{path}: empty uid is not allowed."); return(value); } var xrefSpec = new XRefSpec { Uid = uid }; var parts = schema.XrefProperties ?? new List <string> { "name", "fullName" }; var root = context.GetModel <object>(); foreach (var part in parts.Distinct()) { var jsonPointer = new JsonPointer(path + "/" + part); var property = jsonPointer.GetValue(root); if (property != null) { if (property is string str) { xrefSpec[part] = str; } else { Logger.LogWarning($"Type {property.GetType()} from {jsonPointer} is not supported as the value of xref spec."); } } } if (IsInternalXrefSpec(schema)) { context.Uids.Add(new UidDefinition(uid, context.OriginalFileAndType.FullPath, path: path + "/uid")); xrefSpec.Href = ((RelativePath)context.OriginalFileAndType.File).GetPathFromWorkingFolder().UrlEncode().ToString(); context.XRefSpecs.Add(xrefSpec); } else { context.ExternalXRefSpecs.Add(xrefSpec); } return(value); }
public object Interpret(BaseSchema schema, object value, IProcessContext context, string path) { if (value == null || !CanInterpret(schema)) { return(value); } var uid = JsonPointer.GetChild(value, "uid") as string; if (string.IsNullOrEmpty(uid)) { Logger.LogWarning($"Invalid xrefProperties for {path}: uid is not defined."); return(value); } var xrefSpec = new XRefSpec { Uid = uid }; var parts = schema.XrefProperties ?? new List <string> { "name", "fullName" }; var root = context.Model.Content; foreach (var part in parts.Distinct()) { var jsonPointer = new JsonPointer(path + "/" + part); var property = jsonPointer.GetValue(root); if (property != null) { if (property is string str) { xrefSpec[part] = str; } else { Logger.LogWarning($"Type {property.GetType()} from {jsonPointer} is not supported as the value of xref spec."); } } } if (IsInternalXrefSpec(schema)) { context.Properties.Uids.Add(new UidDefinition(uid, context.Model.LocalPathFromRoot, path: path + "/uid")); xrefSpec.Href = ((RelativePath)context.Model.Key).UrlEncode().ToString(); context.Properties.XRefSpecs.Add(xrefSpec); } else { context.Properties.ExternalXRefSpecs.Add(xrefSpec); } return(value); }
public void TestJsonPointerWithComplexObject() { var root = ConvertToObjectHelper.ConvertToDynamic(ConvertToObjectHelper.ConvertJObjectToObject(JsonUtility.FromJsonString <object>(@" { ""dict"": { ""key1"": ""value1"", ""key2"": [""arr1"", ""arr2""], ""key3"": { ""key1"": ""value1"", ""key2"": [""arr1"", ""arr2""], ""key3"": { ""key1"": ""value1"", ""key2"": [""arr1"", ""arr2""], ""key3"": { ""key1"": ""value1"" } } } }, ""array"": [""bar"", ""baz""] } "))); Assert.Equal(root, new JsonPointer("").GetValue(root)); Assert.Equal("value1", new JsonPointer("/dict/key1").GetValue(root)); Assert.Equal("arr2", new JsonPointer("/dict/key2/1").GetValue(root)); Assert.Equal("value1", new JsonPointer("/dict/key3/key3/key3/key1").GetValue(root)); Assert.Null(new JsonPointer("/dict/key4").GetValue(root)); Assert.Null(new JsonPointer("/dict/key4/key1").GetValue(root)); Assert.Null(new JsonPointer("/dict/key2/2").GetValue(root)); var jp = new JsonPointer("/dict/key1"); jp.SetValue(ref root, 1); Assert.Equal(1, jp.GetValue(root)); jp = new JsonPointer("/dict/key3/key2/1"); jp.SetValue(ref root, 2); Assert.Equal(2, jp.GetValue(root)); jp = new JsonPointer(""); jp.SetValue(ref root, 3); Assert.Equal(3, root); Assert.Equal(3, jp.GetValue(root)); Assert.Throws <InvalidJsonPointerException>(() => new JsonPointer("/dict/key2/2").SetValue(ref root, 1)); }