Class XapiJson
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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonJsonElementThe JSON value.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
utf8JsonReadOnlySpan<byte>UTF-8 JSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonstringJSON text.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
jsonJsonElementThe JSON value.
versionXapiVersionThe xAPI version whose rules apply.
modeXapiReadModeHow 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
aboutAboutThe About object.
versionXapiVersionThe 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
activityActivityThe Activity.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatUse Canonical with
acceptLanguagesto keep one language per map.acceptLanguagesIReadOnlyList<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
actorActorThe Actor.
versionXapiVersionThe 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
personPersonThe Person.
versionXapiVersionThe 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
statementStatementThe Statement.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatWhich parts of Agents, Groups, Activities and Verbs to include.
acceptLanguagesIReadOnlyList<string>For Canonical: language ranges in order of preference.
Returns
- string
Compact JSON text.
Exceptions
- InvalidOperationException
The Statement uses
contextAgentsorcontextGroupsandversionis 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
statementsIEnumerable<Statement>The Statements.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatWhich parts of Agents, Groups, Activities and Verbs to include.
acceptLanguagesIReadOnlyList<string>For Canonical: language ranges in order of preference.
Returns
- string
Compact JSON text.
Exceptions
- InvalidOperationException
A Statement uses
contextAgentsorcontextGroupsandversionis 1.0.3.
WriteAbout(Utf8JsonWriter, About, XapiVersion)
Writes an About object.
public static void WriteAbout(Utf8JsonWriter writer, About about, XapiVersion version)
Parameters
writerUtf8JsonWriterThe destination.
aboutAboutThe About object.
versionXapiVersionThe 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
writerUtf8JsonWriterThe destination.
activityActivityThe Activity.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatUse Canonical with
acceptLanguagesto keep one language per map.acceptLanguagesIReadOnlyList<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
writerUtf8JsonWriterThe destination.
actorActorThe Actor.
versionXapiVersionThe xAPI version to write for.
WritePerson(Utf8JsonWriter, Person, XapiVersion)
Writes a Person object.
public static void WritePerson(Utf8JsonWriter writer, Person person, XapiVersion version)
Parameters
writerUtf8JsonWriterThe destination.
personPersonThe Person.
versionXapiVersionThe 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
writerUtf8JsonWriterThe destination.
statementStatementThe Statement.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatWhich parts of Agents, Groups, Activities and Verbs to include.
acceptLanguagesIReadOnlyList<string>For Canonical: language ranges in order of preference, like an
Accept-Languageheader.
Exceptions
- InvalidOperationException
The Statement uses
contextAgentsorcontextGroupsandversionis 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
writerUtf8JsonWriterThe destination.
statementsIEnumerable<Statement>The Statements.
versionXapiVersionThe xAPI version to write for.
formatStatementFormatWhich parts of Agents, Groups, Activities and Verbs to include.
acceptLanguagesIReadOnlyList<string>For Canonical: language ranges in order of preference.
Exceptions
- InvalidOperationException
A Statement uses
contextAgentsorcontextGroupsandversionis 1.0.3.