public virtual void FlattenTests(string testId, JsonLdTestType testType, string inputPath, string contextPath, string expectedOutputPath, JsonLdErrorCode expectedErrorCode, string baseIri, string processorMode, string expandContextPath, bool compactArrays, string rdfDirection) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays, rdfDirection); var inputJson = File.ReadAllText(inputPath); var contextJson = contextPath == null ? null : File.ReadAllText(contextPath); var inputElement = JToken.Parse(inputJson); var contextElement = contextJson == null ? null : JToken.Parse(contextJson); switch (testType) { case JsonLdTestType.PositiveEvaluationTest: var expectedOutputJson = File.ReadAllText(expectedOutputPath); var expectedOutputElement = JToken.Parse(expectedOutputJson); var actualOutputElement = JsonLdProcessor.Flatten(inputElement, contextElement, processorOptions); Assert.True(DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing flatten test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); break; case JsonLdTestType.NegativeEvaluationTest: var exception = Assert.Throws <JsonLdProcessorException>(() => JsonLdProcessor.Flatten(inputElement, contextElement, processorOptions)); Assert.Equal(expectedErrorCode, exception.ErrorCode); break; default: Assert.True(false, $"Test type {testType} has not been implemented for Flatten tests"); break; } }
public virtual void JsonLdFramingTests(string testId, JsonLdTestType testType, string inputPath, string framePath, string expectedOutputPath, JsonLdErrorCode expectErrorCode, string processingMode, bool pruneBlankNodeIdentifiers, bool?omitGraph, bool ordered) { var inputJson = File.ReadAllText(inputPath); var frameJson = File.ReadAllText(framePath); var jsonLdProcessingMode = "json-ld-1.0".Equals(processingMode) ? JsonLdProcessingMode.JsonLd10 : JsonLdProcessingMode.JsonLd11FrameExpansion; var options = new JsonLdProcessorOptions { ProcessingMode = jsonLdProcessingMode, PruneBlankNodeIdentifiers = pruneBlankNodeIdentifiers, Ordered = ordered, }; if (omitGraph.HasValue) { options.OmitGraph = omitGraph.Value; } var inputElement = JToken.Parse(inputJson); var frameElement = JToken.Parse(frameJson); switch (testType) { case JsonLdTestType.PositiveEvaluationTest: var expectedOutputJson = File.ReadAllText(expectedOutputPath); var expectedOutputElement = JToken.Parse(expectedOutputJson); var actualOutput = JsonLdProcessor.Frame(inputElement, frameElement, options); Assert.True(DeepEquals(expectedOutputElement, actualOutput), $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutputElement}\nActual:\n{actualOutput}"); break; case JsonLdTestType.NegativeEvaluationTest: var exception = Assert.ThrowsAny <JsonLdProcessorException>(() => JsonLdProcessor.Frame(inputElement, frameElement, options)); Assert.Equal(expectErrorCode, exception.ErrorCode); break; case JsonLdTestType.PositiveSyntaxTest: JsonLdProcessor.Frame(inputElement, frameElement, options); break; case JsonLdTestType.NegativeSyntaxTest: Assert.ThrowsAny <JsonLdProcessorException>(() => JsonLdProcessor.Frame(inputElement, frameElement, options)); break; } }
private static async Task <JArray> ExpandAsync(RemoteDocument doc, Uri documentLocation, JsonLdProcessorOptions options = null) { var activeContext = new JsonLdContext { Base = documentLocation }; if (options.Base != null) { activeContext.Base = options.Base; } var processor = new JsonLdProcessor(options); if (options.ExpandContext != null) { var expandObject = options.ExpandContext as JObject; if (expandObject != null) { var contextProperty = expandObject.Property("@context"); if (contextProperty != null) { activeContext = processor.ProcessContext(activeContext, contextProperty.Value); } else { activeContext = processor.ProcessContext(activeContext, expandObject); } } else { activeContext = processor.ProcessContext(activeContext, options.ExpandContext); } } if (doc.ContextUrl != null) { var contextDoc = await LoadJsonAsync(doc.ContextUrl, options); if (contextDoc.Document is string) { contextDoc.Document = JToken.Parse(contextDoc.Document as string); } activeContext = processor.ProcessContext(activeContext, contextDoc.Document as JToken); } if (doc.Document is string) { doc.Document = JToken.Parse(doc.Document as string); } return(processor.Expand(activeContext, null, doc.Document as JToken)); }
/// <summary> /// Flattens the given input and compacts it using the passed context according to the steps in the JSON-LD Flattening algorithm. /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <param name="options"></param> /// <returns></returns> public static async Task <JToken> FlattenAsync(JToken input, JToken context, JsonLdProcessorOptions options = null) { var processor = new JsonLdProcessor(options); // Set expanded input to the result of using the expand method using input and options. var expandedInput = await ExpandAsync(input, options); // If context is a dictionary having an @context member, set context to that member's value, otherwise to context. var contextObject = context as JObject; if (contextObject?.Property("@context") != null) { context = contextObject["@context"]; } return(processor.FlattenWrapper(expandedInput, context, options?.CompactArrays ?? true)); }
public void FlattenTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri, string processorMode, string expandContextPath, bool compactArrays) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays); var inputJson = File.ReadAllText(inputPath); var contextJson = contextPath == null ? null : File.ReadAllText(contextPath); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var inputElement = JToken.Parse(inputJson); var contextElement = contextJson == null ? null : JToken.Parse(contextJson); var expectedOutputElement = JToken.Parse(expectedOutputJson); var actualOutputElement = JsonLdProcessor.Flatten(inputElement, contextElement, processorOptions); Assert.True(JToken.DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing flatten test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); }
public void JsonLdFramingTests(string inputPath, string framePath, string expectedOutputPath, bool pruneBlankNodeIdentifiers) { var inputJson = File.ReadAllText(inputPath); var frameJson = File.ReadAllText(framePath); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var options = new JsonLdProcessorOptions { PruneBlankNodeIdentifiers = pruneBlankNodeIdentifiers }; var inputElement = JToken.Parse(inputJson); var frameElement = JToken.Parse(frameJson); var expectedOutputElement = JToken.Parse(expectedOutputJson); var actualOutput = JsonLdProcessor.Frame(inputElement, frameElement, options); Assert.True(JToken.DeepEquals(expectedOutputElement, actualOutput), $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutputElement}\nActual:\n{actualOutput}"); }
public void ExpandTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri, string processorMode, string expandContextPath, bool compactArrays) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays); var inputJson = File.ReadAllText(inputPath); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var inputElement = JToken.Parse(inputJson); var expectedOutputElement = JToken.Parse(expectedOutputJson); // Expand tests should not have a context parameter Assert.Null(contextPath); var actualOutputElement = JsonLdProcessor.Expand(inputElement, processorOptions); Assert.True(JToken.DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing expand test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); }
/// <summary> /// Run the Compaction algorithm asynchronously /// </summary> /// <param name="input">The JSON-LD data to be compacted. Expected to be a JObject or JArray of JObject or a JString whose value is the IRI reference to a JSON-LD document to be retrieved</param> /// <param name="context">The context to use for the compaction process. May be a JObject, JArray of JObject, JString or JArray of JString. String values are treated as IRI references to context documents to be retrieved</param> /// <param name="options">Additional processor options</param> /// <returns></returns> public static async Task <JObject> CompactAsync(JToken input, JToken context, JsonLdProcessorOptions options) { var processor = new JsonLdProcessor(options); // Set expanded input to the result of using the expand method using input and options. var expandedInput = await ExpandAsync(input, options); // If context is a dictionary having an @context member, set context to that member's value, otherwise to context. var contextProperty = (context as JObject)?.Property("@context"); if (contextProperty != null) { context = contextProperty.Value; } // Set compacted output to the result of using the Compaction algorithm, passing context as active context, an empty dictionary as inverse context, null as property, expanded input as element, and if passed, the compactArrays flag in options. var compactResult = processor.CompactWrapper(context, new JObject(), null, expandedInput, options.CompactArrays); return(compactResult); }
public virtual void ExpandTests(string testId, JsonLdTestType testType, string inputPath, string contextPath, string expectedOutputPath, JsonLdErrorCode expectErrorCode, string baseIri, string processorMode, string expandContextPath, bool compactArrays, string rdfDirection) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays, rdfDirection); var inputJson = File.ReadAllText(inputPath); var inputElement = JToken.Parse(inputJson); // Expand tests should not have a context parameter Assert.Null(contextPath); switch (testType) { case JsonLdTestType.PositiveEvaluationTest: var actualOutputElement = JsonLdProcessor.Expand(inputElement, processorOptions); var expectedOutputJson = File.ReadAllText(expectedOutputPath); var expectedOutputElement = JToken.Parse(expectedOutputJson); Assert.True(DeepEquals(actualOutputElement, expectedOutputElement), $"Error processing expand test {Path.GetFileName(inputPath)}.\nActual output does not match expected output.\nExpected:\n{expectedOutputElement}\n\nActual:\n{actualOutputElement}"); break; case JsonLdTestType.NegativeEvaluationTest: var exception = Assert.Throws <JsonLdProcessorException>( () => JsonLdProcessor.Expand(inputElement, processorOptions)); Assert.Equal(expectErrorCode, exception.ErrorCode); break; case JsonLdTestType.PositiveSyntaxTest: // Expect test to run without throwing processing errors var _ = JsonLdProcessor.Expand(inputElement, processorOptions); break; case JsonLdTestType.NegativeSyntaxTest: Assert.ThrowsAny <JsonLdProcessorException>(() => JsonLdProcessor.Expand(inputElement, processorOptions)); break; } }