TextApi

JSON Serialization

OperationMapper serializes and deserializes operations to and from JSON. This is the bridge between external systems (LLMs, APIs, config files) and the TextAPI execution engine.

Navigation: Overview Operations Examples Pipeline

JSON Format Rules

Two JSON Formats

Bare Array

A JSON array of operation objects:

[
  { "type": "REPLACE_ALL", "find": "foo", "replace": "bar" },
  { "type": "TRIM_TRAILING_WHITESPACE" },
  { "type": "SORT_LINES", "descending": true }
]

Pipeline Object

A JSON object with an "operations" array (useful when you want to add pipeline-level metadata later):

{
  "operations": [
	{ "type": "REPLACE_ALL", "find": "foo", "replace": "bar" },
	{ "type": "TRIM_TRAILING_WHITESPACE" }
  ]
}

OperationMapper API

Method Description
FromJson(string json) Parse a bare array. Returns IReadOnlyList<IDocumentOperation>. Throws JsonException if root is not an array or a type is unknown.
FromPipelineJson(string json) Parse a pipeline object ({"operations":[...]}). Throws JsonException if the key is missing.
ToJson(IDocumentOperation op) Serialize one operation to a JSON object string.
ToPipelineJson(IEnumerable<IDocumentOperation> ops) Serialize a list of operations to a pipeline object string.

Deserializing

using TextAPI.Operations.Serialization;

// Bare array
var ops = OperationMapper.FromJson("""
[
  {"type":"REPLACE_ALL","find":"TODO","replace":"DONE"},
  {"type":"SORT_LINES"}
]
""");

// Pipeline object
var ops2 = OperationMapper.FromPipelineJson(jsonString);

// Run them
var result = new DocumentPipeline(doc).Add(ops).Execute();

Serializing

// Single op
var op   = new ReplaceAllOperation { Find = "a", Replace = "b", CaseSensitive = false };
string json = OperationMapper.ToJson(op);
// {"type":"REPLACE_ALL","find":"a","replace":"b","caseSensitive":false,"wholeWord":false}

// List of ops as pipeline JSON
IDocumentOperation[] ops = [
	new AppendOperation  { Text = " World" },
	new PrependOperation { Text = "Hello" }
];
string pipeline = OperationMapper.ToPipelineJson(ops);

Usage with DocumentPipeline

// From external source (API, file, LLM output)
string externalJson = File.ReadAllText("operations.json");

// Deserialize and execute
var ops = OperationMapper.FromPipelineJson(externalJson);
var result = new DocumentPipeline(doc)
	.Add(ops)
	.Execute();