private static void RemoveEmbed(FramingState state, string id) { var embed = state.GetEmbeddedNode(id); if (embed == null) { return; } var parent = embed.Item1; var property = embed.Item2; var subject = new JObject(new JProperty("@id", id)); if (parent is JArray parentArray) { for (var i = 0; i < parentArray.Count; i++) { var item = parentArray[i]; if (item is JObject itemObject && itemObject.ContainsKey("@id") && itemObject["@id"].Value <string>().Equals(id)) { parent[i] = subject; break; } } } else { var useArray = JsonLdUtils.IsArray(parent[property]); JsonLdUtils.RemoveValue(parent as JObject, property, subject, useArray); JsonLdUtils.AddValue(parent as JObject, property, subject, useArray); } }
/// <summary> /// Determine if the specified token is a valid frame object. /// </summary> /// <param name="value">The token to validate.</param> /// <exception cref="JsonLdFramingException">Raised if <paramref name="value"/> is not a valid frame object.</exception> private static void ValidateFrame(JToken value) { // 1.1 - Frame MUST be a map. if (!(value is JObject obj)) { throw new JsonLdProcessorException(JsonLdErrorCode.InvalidFrame, $"Invalid frame. The frame must be a map."); } // 1.2 - If frame has an @id entry, its value MUST be either an array containing a single empty map as a // value, a valid IRI or an array where all values are valid IRIs. if (obj.ContainsKey("@id")) { var idValue = obj["@id"]; if (!(IsEmptyMapArray(idValue) || JsonLdUtils.IsIri(idValue) || JsonLdUtils.IsArray(idValue, JsonLdUtils.IsIri))) { throw new JsonLdProcessorException(JsonLdErrorCode.InvalidFrame, "Invalid frame. The value of the @id property must be either " + "an array containing a single empty map; an IRI string or an array of IRI strings."); } } // 1.3 - If frame has a @type entry, its value MUST be either an array containing a single empty map as a // value, an array containing a map with a entry whose key is @default, a valid IRI or an array where all // values are valid IRIs. if (obj.ContainsKey("@type")) { var typeValue = obj["@type"]; if (!(IsEmptyMapArray(typeValue) || IsDefaultObjectArray(typeValue) || JsonLdUtils.IsIri(typeValue) || JsonLdUtils.IsArray(typeValue, JsonLdUtils.IsIri))) { throw new JsonLdProcessorException(JsonLdErrorCode.InvalidFrame, "Invalid frame. The value of the @type property must be either " + "an array containing a single empty map, " + "an array containing a single map with an @default key, an IRI string, " + "or an array of IRI strings."); } } }