Table of Contents

Class XapiJson

Namespace
Xapi.Json
Assembly
Xapi.Core.dll

Reads and writes xAPI JSON without reflection or source generators, so it works under trimming, NativeAOT, and IL2CPP.

public static class XapiJson
Inheritance
XapiJson
Inherited Members

Remarks

Reading collects every error with a JSON path instead of stopping at the first. In Strict mode the result is also validated with StatementValidator; validation runs only when the JSON could be turned into a model, so a document with structural errors reports those first.

Writing emits only properties the standard defines, timestamps in UTC with fixed fraction digits, and extension values exactly as read. Agents and Groups always carry objectType; Activities carry it in the object position only.

Methods

ParseAbout(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads an About object (the response of the About resource).

public static XapiParseResult<About> ParseAbout(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read. Use Lenient for responses of unknown LRSs.

Returns

XapiParseResult<About>

The About object, or every error found.

ParseAbout(string, XapiVersion, XapiReadMode)

Reads an About object.

public static XapiParseResult<About> ParseAbout(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<About>

The About object, or every error found.

ParseActivity(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads an Activity, for example a response of the Activities resource.

public static XapiParseResult<Activity> ParseActivity(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Activity>

The Activity, or every error found.

ParseActivity(string, XapiVersion, XapiReadMode)

Reads an Activity.

public static XapiParseResult<Activity> ParseActivity(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Activity>

The Activity, or every error found.

ParseActor(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads an Agent or Group, for example the agent query parameter.

public static XapiParseResult<Actor> ParseActor(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Actor>

The Actor, or every error found.

ParseActor(string, XapiVersion, XapiReadMode)

Reads an Agent or Group.

public static XapiParseResult<Actor> ParseActor(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Actor>

The Actor, or every error found.

ParsePerson(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads a Person object (a response of the Agents resource).

public static XapiParseResult<Person> ParsePerson(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Person>

The Person, or every error found.

ParsePerson(string, XapiVersion, XapiReadMode)

Reads a Person object.

public static XapiParseResult<Person> ParsePerson(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Person>

The Person, or every error found.

ParseStatement(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads one Statement.

public static XapiParseResult<Statement> ParseStatement(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Statement>

The Statement, or every error found.

ParseStatement(string, XapiVersion, XapiReadMode)

Reads one Statement.

public static XapiParseResult<Statement> ParseStatement(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Statement>

The Statement, or every error found.

ParseStatement(JsonElement, XapiVersion, XapiReadMode)

Reads one Statement from an already parsed JSON value.

public static XapiParseResult<Statement> ParseStatement(JsonElement json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json JsonElement

The JSON value.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<Statement>

The Statement, or every error found.

ParseStatements(ReadOnlySpan<byte>, XapiVersion, XapiReadMode)

Reads a Statement batch: a JSON array of Statements, or a single Statement object.

public static XapiParseResult<XapiList<Statement>> ParseStatements(ReadOnlySpan<byte> utf8Json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

utf8Json ReadOnlySpan<byte>

UTF-8 JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<XapiList<Statement>>

The Statements, or every error found. Paths start with the array index, for example $[2].actor.

Remarks

In strict mode the batch is validated with ValidateBatch(IReadOnlyList<Statement>, XapiVersion), which also rejects duplicate ids.

ParseStatements(string, XapiVersion, XapiReadMode)

Reads a Statement batch: a JSON array of Statements, or a single Statement object.

public static XapiParseResult<XapiList<Statement>> ParseStatements(string json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json string

JSON text.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<XapiList<Statement>>

The Statements, or every error found.

ParseStatements(JsonElement, XapiVersion, XapiReadMode)

Reads a Statement batch from an already parsed JSON value (an array or a single object).

public static XapiParseResult<XapiList<Statement>> ParseStatements(JsonElement json, XapiVersion version, XapiReadMode mode = XapiReadMode.Strict)

Parameters

json JsonElement

The JSON value.

version XapiVersion

The xAPI version whose rules apply.

mode XapiReadMode

How strictly to read.

Returns

XapiParseResult<XapiList<Statement>>

The Statements, or every error found.

SerializeAbout(About, XapiVersion)

Serializes an About object.

public static string SerializeAbout(About about, XapiVersion version)

Parameters

about About

The About object.

version XapiVersion

The xAPI version to write for.

Returns

string

Compact JSON text.

SerializeActivity(Activity, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Serializes an Activity (with objectType).

public static string SerializeActivity(Activity activity, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

activity Activity

The Activity.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Use Canonical with acceptLanguages to keep one language per map.

acceptLanguages IReadOnlyList<string>

Language ranges in order of preference.

Returns

string

Compact JSON text.

SerializeActor(Actor, XapiVersion)

Serializes an Agent or Group, for example for the agent query parameter.

public static string SerializeActor(Actor actor, XapiVersion version)

Parameters

actor Actor

The Actor.

version XapiVersion

The xAPI version to write for.

Returns

string

Compact JSON text.

SerializePerson(Person, XapiVersion)

Serializes a Person object.

public static string SerializePerson(Person person, XapiVersion version)

Parameters

person Person

The Person.

version XapiVersion

The xAPI version to write for.

Returns

string

Compact JSON text.

SerializeStatement(Statement, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Serializes one Statement to a JSON string.

public static string SerializeStatement(Statement statement, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

statement Statement

The Statement.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Which parts of Agents, Groups, Activities and Verbs to include.

acceptLanguages IReadOnlyList<string>

For Canonical: language ranges in order of preference.

Returns

string

Compact JSON text.

Exceptions

InvalidOperationException

The Statement uses contextAgents or contextGroups and version is 1.0.3.

SerializeStatements(IEnumerable<Statement>, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Serializes Statements to a JSON array string.

public static string SerializeStatements(IEnumerable<Statement> statements, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

statements IEnumerable<Statement>

The Statements.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Which parts of Agents, Groups, Activities and Verbs to include.

acceptLanguages IReadOnlyList<string>

For Canonical: language ranges in order of preference.

Returns

string

Compact JSON text.

Exceptions

InvalidOperationException

A Statement uses contextAgents or contextGroups and version is 1.0.3.

WriteAbout(Utf8JsonWriter, About, XapiVersion)

Writes an About object.

public static void WriteAbout(Utf8JsonWriter writer, About about, XapiVersion version)

Parameters

writer Utf8JsonWriter

The destination.

about About

The About object.

version XapiVersion

The xAPI version to write for.

WriteActivity(Utf8JsonWriter, Activity, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Writes an Activity (with objectType).

public static void WriteActivity(Utf8JsonWriter writer, Activity activity, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

writer Utf8JsonWriter

The destination.

activity Activity

The Activity.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Use Canonical with acceptLanguages to keep one language per map.

acceptLanguages IReadOnlyList<string>

Language ranges in order of preference.

WriteActor(Utf8JsonWriter, Actor, XapiVersion)

Writes an Agent or Group.

public static void WriteActor(Utf8JsonWriter writer, Actor actor, XapiVersion version)

Parameters

writer Utf8JsonWriter

The destination.

actor Actor

The Actor.

version XapiVersion

The xAPI version to write for.

WritePerson(Utf8JsonWriter, Person, XapiVersion)

Writes a Person object.

public static void WritePerson(Utf8JsonWriter writer, Person person, XapiVersion version)

Parameters

writer Utf8JsonWriter

The destination.

person Person

The Person.

version XapiVersion

The xAPI version to write for.

WriteStatement(Utf8JsonWriter, Statement, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Writes one Statement.

public static void WriteStatement(Utf8JsonWriter writer, Statement statement, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

writer Utf8JsonWriter

The destination.

statement Statement

The Statement.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Which parts of Agents, Groups, Activities and Verbs to include.

acceptLanguages IReadOnlyList<string>

For Canonical: language ranges in order of preference, like an Accept-Language header.

Exceptions

InvalidOperationException

The Statement uses contextAgents or contextGroups and version is 1.0.3.

WriteStatements(Utf8JsonWriter, IEnumerable<Statement>, XapiVersion, StatementFormat, IReadOnlyList<string>?)

Writes Statements as a JSON array.

public static void WriteStatements(Utf8JsonWriter writer, IEnumerable<Statement> statements, XapiVersion version, StatementFormat format = StatementFormat.Exact, IReadOnlyList<string>? acceptLanguages = null)

Parameters

writer Utf8JsonWriter

The destination.

statements IEnumerable<Statement>

The Statements.

version XapiVersion

The xAPI version to write for.

format StatementFormat

Which parts of Agents, Groups, Activities and Verbs to include.

acceptLanguages IReadOnlyList<string>

For Canonical: language ranges in order of preference.

Exceptions

InvalidOperationException

A Statement uses contextAgents or contextGroups and version is 1.0.3.