Serialization to Part 10
serializeDicom(ds, options) writes a Dataset back to Part 10 bytes. By default it writes the
Data Set as it was read: the same elements in the same order, a value read at an odd length at
that length, so a parse and a write change nothing you did not change. { normalize: true } asks
it to normalize the Data Set instead: ascending tag order, even lengths, and no retired group
lengths. Neither mode corrects a value.
Every DICOM object on this page is synthetic (an invented patient, obviously-fake UIDs) and encoded as a base64 buffer, so an example needs no file on disk. Never paste a real object into a doc or a test: a real object is PHI, and one committed to a repository is a leak the moment it publishes.
What the writer guarantees
These hold in both modes.
- Preamble and
DICM. A 128-byte preamble and the magic, always, even for an object parsed from bytes that had neither (DICOM_MISSING_PREAMBLEon the way in). - A recomputed File Meta group. Always Explicit VR LE, with
(0002,0000)File Meta Information Group Length recomputed from what is actually written rather than copied from the source. - The dataset body in the source transfer syntax. The Transfer Syntax UID on the object decides
every byte of the encoding, and the writer does not transcode. Under every encapsulation syntax
PS3.5 2026c section A.4 names (Compatibility lists them), the Data Set is
Explicit VR LE and the top-level Pixel Data is
OBof undefined length: the source's Basic Offset Table and fragment Items byte for byte and in order, then a zero-length Sequence Delimitation Item. A top-level Pixel Data section A.4 does not allow is refused, not repaired (below). - A value too long for a 16-bit Value Length Field goes out as
UN. Under Explicit VR, a value past 65,534 bytes in a VR whose length field is 16 bits is written with VRUNin the long form, as PS3.5 2026c §6.2.2 requires, and reads back asUN; a Private Creator or File Meta element, for which that section forbidsUN, is refused withDICOM_SERIALIZE_VALUE_TOO_LONG_FOR_VRinstead. - Byte-for-byte passthrough of what it must not touch. Encapsulated Pixel Data fragments and
UNvalues are re-emitted as read; the writer never re-encodes pixels. - A fixed point. Serializing an already-serialized object returns the same bytes, in either mode.
As read, the default
- Every Data Set in the order it was read, the root and every Item's, at every depth. Nothing moves.
- An odd length the parser read is written at that length. A value built in code, such as one
deidentify()replaced or anElementyou built, is padded to even length with the pad byte its VR specifies, because nothing read it at an odd length. - A retired group length
(gggg,0000)is kept while it still holds: while its value equals the encoded length of its group's other elements as written. One a change made false, such asdeidentify()removing an element of its group, is left out rather than written stale.
With { normalize: true }
- Even-length Value Fields. PS3.5 2026c §7.1.1 defines a Value Field as "an even number of bytes containing the Value(s) of the Data Element", so odd values are padded with the pad byte their VR specifies. An odd length that arrived tolerated goes out even.
- Retired group lengths left out, as PS3.5 2026c §7.2 retires them.
- Every Data Set in ascending tag order. PS3.5 2026c §7.1 says the Data Elements in a Data Set
"shall be ordered by increasing Data Element Tag Number", and PS3.5 §7.5.1 says the same inside every
Item. The writer emits the root that way, and the Data Set of every Item of every Sequence it can
walk on the wire, at every depth up to
NESTING_DEPTH_LIMIT, whatever order the source file, your code ordeidentify()left it in. Items stay in their order (PS3.5 §7.5), only whole elements move, so no value byte changes, and nothing is written that the Sequence's own bytes did not carry. A Sequence is re-ordered only where its parseditemsmatch its bytes, so the output reads back as the source did.- Not ordered, and said here rather than found later: a tag repeated inside an Item is kept
twice, in source order, so that output still breaks the "at most once" of PS3.5 2026c §7.1; a
Sequence whose Item stream cannot be walked to exactly its end (a length that runs past its
container, an undefined-length Item with no Item Delimitation Item, bytes that are not an Item
stream), one nested past the bound, one whose parsed
itemsdo not match its bytes (a Sequence the parser did not descend,DICOM_SQ_NOT_DESCENDED, for one), and anyUN-carried Sequence (under Implicit VR LE that includes a private Sequence inside an Item, even one aDicomProfileresolved toSQ, since a default read resolves its tag toUN) are written as read, unordered; and an element the parser relocated because a length field lied is ordered where the parser placed it, since ordering cannot recover an order the source destroyed. - Written last, even when its tag sorts earlier: an element whose own bytes do not show where a
reader ends it, such as an undefined-length
UNthe parser could not read as a Sequence, or a Sequence,UN, or Pixel Data value missing its Sequence Delimitation Item. A reader takes whatever follows such a value into it, so it keeps its place after the ascending rest of its Data Set, at the root and inside an Item, rather than losing the elements after it on the next read. A Sequence nested past the bound goes after the ascending rest of its Item too, unless it is a defined-length Sequence under Implicit VR LE: seeing where a reader ends any other would take the walk past the bound.
- Not ordered, and said here rather than found later: a tag repeated inside an Item is kept
twice, in source order, so that output still breaks the "at most once" of PS3.5 2026c §7.1; a
Sequence whose Item stream cannot be walked to exactly its end (a length that runs past its
container, an undefined-length Item with no Item Delimitation Item, bytes that are not an Item
stream), one nested past the bound, one whose parsed
import { parseDicom, serializeDicom } from "@cosyte/dicom";
// Synthetic object with the preamble omitted: a quirk the parser tolerates.
const buf = Buffer.from(
"AgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xAAgAYABDUwIAQ1QQACAATE8GAE1STi00Mg==",
"base64",
);
const ds = parseDicom(buf);
ds.warnings.length > 0; // => true
// Preamble, DICM and a recomputed File Meta, whichever mode writes it.
const out = serializeDicom(ds);
Buffer.from(out.subarray(128, 132)).toString("ascii"); // => "DICM"
// The data survives the round trip...
parseDicom(out).patient.id; // => "MRN-42" /* synthetic */
// ...the output no longer carries the deviation...
parseDicom(out).warnings.length; // => 0
// ...and re-serializing is byte-identical: the writer is a fixed point.
Buffer.from(serializeDicom(parseDicom(out))).equals(out); // => true
// Normalized, the Data Set is ascending and even, and still reads back the same.
parseDicom(serializeDicom(ds, { normalize: true })).patient.id; // => "MRN-42" /* synthetic */
When it throws, and what the error carries
The writer has its own error taxonomy, separate from the parser's DICOM_FATAL_CODES (which are locked to
read-side structural corruption) and from the value layer's DicomValueError. It throws only with
a code listed below.
| Export | What it is |
|---|---|
SERIALIZE_ERROR_CODES | The frozen registry of the writer's codes: DICOM_SERIALIZE_MISSING_TRANSFER_SYNTAX, DICOM_SERIALIZE_UNSUPPORTED_TRANSFER_SYNTAX, DICOM_SERIALIZE_INVALID_ENCAPSULATED_PIXEL_DATA, DICOM_SERIALIZE_DIRECTORY_OFFSET_UNRESOLVED, DICOM_SERIALIZE_DIRECTORY_OFFSET_DEFLATED, DICOM_SERIALIZE_VALUE_TOO_LONG_FOR_VR. |
SerializeErrorCode | The discriminant type over that registry, so a switch on a caught code is exhaustive. |
DicomSerializeError | The thrown class. Narrow with err instanceof DicomSerializeError, then on err.code. |
SerializeDicomOptions | The options serializeDicom takes: normalize, off by default. |
DICOM_SERIALIZE_MISSING_TRANSFER_SYNTAX means the Dataset carries no fileMeta, or its transferSyntaxUID is
empty. The Transfer Syntax UID is the dispatch input that decides every byte of the encoding, so
there is no safe default to fall back to and none is invented. DICOM_SERIALIZE_UNSUPPORTED_TRANSFER_SYNTAX means
the UID is outside the set this package writes, the four native syntaxes and the section A.4 ones;
the writer never transcodes, so it cannot emit a syntax it does not understand. That includes the
four JPIP Referenced syntaxes, which parseDicom reads for metadata and this writer refuses.
DICOM_SERIALIZE_INVALID_ENCAPSULATED_PIXEL_DATA means the UID is a PS3.5 2026c section A.4 one and the top-level
Data Set is not one that syntax may carry, so the writer returns nothing rather than repairing it:
no top-level Pixel Data (7FE0,0010); Float or Double Float Pixel Data (7FE0,0008) /
(7FE0,0009) present; Pixel Data with a defined Value Length (Native Format); a fragment stream not
ended by its Sequence Delimitation Item, including one parseDicom read with
DICOM_PIXEL_DATA_FRAGMENTS_NOT_DELIMITED (appending a delimiter to a stream that may be short would
write a well-formed file missing frames); anything other than an Item of defined length before that
delimiter; an Item of odd length; a fragment Item of length zero after the Basic Offset Table; or no
Basic Offset Table Item or no fragment Item. Pixel Data inside a Sequence Item, such as an Icon Image
Sequence, is written as read and not checked.
DICOM_SERIALIZE_DIRECTORY_OFFSET_UNRESOLVED and DICOM_SERIALIZE_DIRECTORY_OFFSET_DEFLATED are the DICOMDIR codes. For a Dataset
whose (0002,0002) is 1.2.840.10008.1.3.10, the writer writes (0004,1200), (0004,1202), and each
Directory Record's (0004,1400) and (0004,1420) as the byte offset, counted from the first byte of
the preamble it writes, of the record each named when the file was read, as PS3.3's Basic Directory
IOD defines them (PS3.3 is not vendored here, so no clause is claimed for it).
That is how a record stays named after the File Meta group is rebuilt, a record's elements are put in
ascending order, or deidentify() re-encodes every record at a new length. A record's identity is
Item.fileOffset, where the parser found its Item tag, which deidentify() carries to the Item it
rebuilds; an offset is tied to the Directory Record Sequence Item whose fileOffset it equals and to
nothing found by scanning for an Item tag. DICOM_SERIALIZE_DIRECTORY_OFFSET_UNRESOLVED means an offset cannot be
tied: a non-zero value naming no Item of the Directory Record Sequence the Dataset holds (one
parseDicom warned about, one left after the Sequence was removed or emptied, one on a hand-built
Dataset with no records) or a value that is not one 32-bit unsigned integer. It is refused rather
than written stale, which would name the wrong bytes, or written as zero, which would silently prune
the tree. DICOM_SERIALIZE_DIRECTORY_OFFSET_DEFLATED means the DICOMDIR is written under Deflated Explicit VR LE with
a non-zero offset, which names no Item a reader of a deflated stream can seek to; one whose offsets
are all zero is written. The limits of the record model are on
Known limitations.
DICOM_SERIALIZE_VALUE_TOO_LONG_FOR_VR means a Private Creator Data Element under an Explicit VR syntax, or a File
Meta Information element under any (the File Meta group is always Explicit VR Little Endian), has a
value past 65,534 bytes in a VR with a 16-bit Value Length Field: PS3.5 2026c §6.2.2 forbids the
UN every other such value is written as, so the value has no encoding and nothing is written.
deidentify() throws it too when it re-encodes a Sequence Item that keeps such a creator.
A DicomSerializeError message is built from the code and fixed structural text and nothing else:
no Transfer Syntax UID, length, or byte from the Dataset reaches it, so, like a DicomParseError,
it holds no source bytes.
What the writer will not do
These are boundaries, not defects. The full list of package non-goals is on Known limitations; what follows is the part that belongs to the writer.
- No transcode. The body is re-emitted in the transfer syntax the object arrived in. Converting between syntaxes is a different operation with different failure modes, and this package does not perform it.
- No pixel re-encoding. Encapsulated fragments pass through byte for byte. Pixel decode and
encode are
@cosyte/dicom-pixel. - Only the typed
FileMetafields round-trip through the model. The group is recomputed spec-clean on emit. Non-modeled(0002,xxxx)elements the source carried are preserved verbatim and re-emitted in ascending tag order, so a repeated non-modeled element is re-emitted repeated; that is unchanged behavior and is recorded here rather than fixed silently. - The byte-for-byte File Meta round trip is scoped to parse-then-serialize. It does not hold for de-identified output, because a de-identified File Meta group describes the de-identifying application instead of the source (PS3.15 §E.1.1). That is a deliberate fidelity loss and the report records it.
- A repeated tag is not recoverable here. A parsed Data Set is a
Map<Tag, Element>, so where the source wrote one tag twice the earlier element was already gone before the writer saw it (DICOM_DUPLICATE_TAG_IN_DATA_SETon the parse). Serializing does not restore it, and no value is invented for it.
The DICOM JSON Model
toDicomJson(ds) writes a Data Set as the DICOM JSON Model of PS3.18 Annex F: one JSON object
keyed by tag, eight uppercase hex digits, each attribute with its vr and its value as Value,
InlineBinary or BulkDataURI. fromDicomJson(json, { fileMeta }) reads such an object back
into a Dataset, which serializeDicom writes as Part 10. The JSON Model holds no File Meta, so
fromDicomJson takes the File Meta to write under, and its Transfer Syntax UID picks the encoding.
Write the text with stringifyDicomJson. The annex orders a Data Set's attributes by tag,
ascending. toDicomJson adds them in that order at every depth, but a JavaScript object lists an
all-digit key with no leading zero first, whatever order it was added in, so JSON.stringify
writes a tag such as 60000010 (Overlay Rows) or 20000010 (Number of Copies) ahead of
00080005. stringifyDicomJson(json, space?) writes the same text as JSON.stringify with every
Data Set's attributes in ascending order of their tags, whatever the case of their hex letters;
where the object already lists them so, the two give the same text. A toJSON method is called as
JSON.stringify calls it, once, with its key, and what it returns is written in its place.
JSON.parse reads either back.
import {
fromDicomJson,
parseDicom,
serializeDicom,
stringifyDicomJson,
toDicomJson,
} from "@cosyte/dicom";
// Synthetic object: Modality CT and an invented Patient ID.
const buf = Buffer.from(
"AgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xAAgAYABDUwIAQ1QQACAATE8GAE1STi00Mg==",
"base64",
);
const ds = parseDicom(buf);
const json = toDicomJson(ds);
json["00080060"]; // => { vr: "CS", Value: ["CT"] }
json["00100020"]; // => { vr: "LO", Value: ["MRN-42"] } /* synthetic */
// The JSON Model carries no File Meta, so the source's goes back in.
const fileMeta = ds.fileMeta ?? { transferSyntaxUID: "1.2.840.10008.1.2.1" };
const back = fromDicomJson(JSON.parse(stringifyDicomJson(json)), { fileMeta });
// Part 10 to JSON to Part 10 writes the same bytes.
Buffer.from(serializeDicom(back)).equals(serializeDicom(ds)); // => true
How each VR is written, and read back the same way:
| VR | JSON form |
|---|---|
AE AS CS DA DT LO SH TM UC UI | Value: strings split on backslashes, padding removed, null for an empty value of several |
LT ST UT UR | Value: one string, a backslash kept as a character |
PN | Value: objects of Alphabetic, Ideographic and Phonetic component groups |
DS IS | Value: JSON numbers of the text's exact decimal value, and a DS no number holds as a JSON string of its text; strings read as text |
US SS UL SL FL FD | Value: JSON numbers |
AT | Value: tags as eight uppercase hex digits |
SQ | Value: nested Data Sets, {} for an empty Item |
OB OD OF OL OV OW UN | InlineBinary (base64), or BulkDataURI where the bulkDataUri option gives one; bulk values inline only with inlineBulkData: true |
| encapsulated Pixel Data | BulkDataURI only, where the bulkDataUri option gives one: the annex has no inline form for a fragment stream |
The shapes are typed: a DicomJsonDataset maps each tag to a DicomJsonAttribute, whose Value
is an array of DicomJsonValue, a Person Name value being a DicomJsonPersonName. The options are
ToDicomJsonOptions and FromDicomJsonOptions, and a refusal's code is a DicomJsonErrorCode.
An attribute with an empty value has no value field. Text is decoded through (0008,0005)
Specific Character Set by the same decoder element.value uses, so the two never disagree;
(0008,0005) is kept as it is, and text is encoded back under it. A BulkDataURI comes back
through the resolveBulkData option of fromDicomJson; one it does not resolve is refused, and
one it throws for is refused with DICOM_JSON_CONVERSION_FAILED.
BulkDataURI on text and number VRs. The annex allows a BulkDataURI on DS, FL, FD,
IS, LT, OB, OD, OF, OL, OV, OW, SL, SS, ST, SV, UC, UL, UN, US,
UT and UV, and fromDicomJson takes one on each: a US lookup table or an FL list by URI is
handed to resolveBulkData with its VR, as a binary value is. On any other VR, or beside a
Value, a BulkDataURI is refused with DICOM_JSON_INVALID_JSON; InlineBinary stays with the seven binary
VRs. Text fetched by URI is written as the resolver gives it, in the Data Set's character set,
and held to the checks the same text given as a Value is held to, with the same codes: a DS or
IS that is not one, or is longer than its VR allows, and a control character its VR does not
allow are refused with DICOM_JSON_MALFORMED_VALUE, an IS out of range with DICOM_JSON_UNREPRESENTABLE_NUMBER, and
bytes its Specific Character Set does not define with DICOM_JSON_UNSUPPORTED_CHARACTER_SET (bytes that are
not UTF-8 under ISO_IR 192, which no JSON text can be, with DICOM_JSON_MALFORMED_VALUE). Bytes for a VR of
numbers that are not a whole number of values are refused with DICOM_JSON_MALFORMED_VALUE.
Byte order. A DICOMweb service serves uncompressed bulk data in Little Endian byte order
(PS3.18, in the retrieve section on bulk data), so fromDicomJson takes the resolver's bytes as
Little Endian: under Explicit VR Big Endian it swaps each value of OW, US and SS (two
bytes), OF, OL, UL, SL and FL (four) and OD, OV, FD, SV and UV (eight), and
leaves OB, UN and text as they are. Bytes that are not a whole number of values of their VR
are refused there with DICOM_JSON_MALFORMED_VALUE. An InlineBinary is another matter: the annex defines it
as the base64 of the Value Field and leaves its byte order to PS3.19, which names none, so an
OW, OF, OD, OL or OV InlineBinary under Big Endian is still refused with
DICOM_JSON_UNSUPPORTED_BYTE_ORDER rather than guessed at. toDicomJson refuses those VRs under Big Endian
too, by URI as well, since the bytes it would hand out are the Data Set's own.
Specific Character Sets carried. One value of the default repertoire (no value, an empty one,
or ISO_IR 6), of a single-byte set without code extensions (ISO_IR 100, 101, 109, 110,
144, 127, 126, 138, 148, 203, 13 and 166), or ISO_IR 192. Both converters refuse
any Data Set, at any depth, whose (0008,0005) has more than one value or names another set (an
ISO 2022 term, GB18030, GBK) with DICOM_JSON_UNSUPPORTED_CHARACTER_SET, even when it holds no text: the
JSON Model has no place for the escape sequences of the code extensions of ISO 2022, and a converter that
read them differently from the reader would change a name without a word. fromDicomJson writes
text outside 7-bit ASCII only under the default repertoire and ISO_IR 100 (as Latin-1) and
ISO_IR 192 (as UTF-8); under the other single-byte sets it writes 7-bit ASCII alone. It then
reads every text value back through the reader's decoder, at every depth, and refuses with
DICOM_JSON_UNSUPPORTED_CHARACTER_SET any that does not read back as the text the JSON gave. The reader
decodes each single-byte set by its own table and reads a byte the set does not define as
U+FFFD, so that refuses any character past U+007F under the default repertoire and ISO_IR 6,
which is ISO-IR 6 and has none, and the C1 control characters (U+0080 to U+009F) under
ISO_IR 100, which PS3.5 section 6.1.1 says shall not be used. Under ISO_IR 192 a lone
surrogate, which UTF-8 cannot encode, is refused too.
A Data Set that declares no set. Text with no (0008,0005), or an empty one, is ISO-IR 6, so
toDicomJson refuses a Latin-1 letter there with DICOM_JSON_MALFORMED_VALUE. Pass fallbackCharacterSet
(for example "ISO_IR 100") to read that text under a set you name instead, and
fallbackCharacterSetForUnknown: true to read under it also where (0008,0005) names a term we
do not decode; no (0008,0005) is added to the JSON. A Data Set parseDicom read with the same
option already carries the set and needs none here. To read the JSON back with fromDicomJson,
give it an (0008,0005) of ISO_IR 100 or ISO_IR 192, since the default repertoire holds no
character past U+007F. See Character sets.
Bulk values are not written inline by default. The model is for metadata, so Pixel Data
(7FE0,0010), Float and Double Float Pixel Data (7FE0,0008) and (7FE0,0009), Overlay Data
(60xx,3000), Waveform Data (5400,1010), Spectroscopy Data (5600,0020) and Encapsulated
Document (0042,0011) need a URI from the bulkDataUri option, at any depth. With none, and
without inlineBulkData: true, toDicomJson refuses the call with DICOM_JSON_BULK_DATA_URI_REQUIRED
rather than leave the value out. Every other binary value is written inline unless bulkDataUri
gives it a URI.
Exact, or refused. A converter gives back exactly the value it was given, or refuses with a code; it never writes a value it cannot carry faithfully:
- Implicit VR. Implicit VR Little Endian writes no VR, and the reader takes the one PS3.5 and
the dictionary give the tag,
UNfor a private one.fromDicomJsonreads the Data Set it wrote back and refuses withVR_MISMATCHany attribute, at any depth, whose VR came back other than the JSON's: Smallest Image Pixel ValueSS-1 with no Pixel Representation of 1 would read asUS65535, and a privateDSasUN. A private Sequence of defined length reads asUNtoo; write it withsequenceLength: "undefined", or under Explicit VR. - Delimiters. One value holding a backslash in any string VR but
LT,ST,UTandUR, or an=or a backslash in a Person Name group, is refused withDICOM_JSON_INVALID_JSONrather than split into several values or groups. A^inside a group is a component delimiter and is kept. DSandISnumbers. A JSON number is a double, so aDSwhose decimal value no double holds exactly, such as9007199254740993(past 2^53) or1e400, is written as a JSON string of its text, which the annex allows, rather than rounded:toDicomJsongives{ vr: "DS", Value: ["9007199254740993"] }, andfromDicomJsonwrites that text back. The string drops the value's leading and trailing padding spaces, and is written only where it is aDSof at most the 16 bytes PS3.5 allows, which is allfromDicomJsonreads; a longer one, such as0.10000000000000001, is refused withDICOM_JSON_UNREPRESENTABLE_NUMBER. A double holds everyISof the 12 characters PS3.5 allows it; a longer one no double holds is refused withDICOM_JSON_UNREPRESENTABLE_NUMBER. Only the form of the text may change.FLnumbers. A JSON number is rounded to the nearest float32, as anyFLwriter rounds it. One past float32's range, or below its smallest normal value and not a float32 itself, is refused withDICOM_JSON_UNREPRESENTABLE_NUMBER:1e-50would otherwise be written as 0.- Person Name groups. A Person Name of more than three component groups is refused with
DICOM_JSON_MALFORMED_VALUE, never truncated: PS3.5 section 6.2 says "There shall be no more than two component group delimiters". - Text bytes. Bytes the Specific Character Set does not decode, such as invalid UTF-8 under
ISO_IR 192, are refused withDICOM_JSON_MALFORMED_VALUErather than read as U+FFFD, and a leading byte order mark is kept as U+FEFF. - Control characters. PS3.5 section 6.2 allows TAB, LF, FF and CR in
LT,STandUT, no control character in any other string VR, and ESC only for code extensions, which are not carried.fromDicomJsonrefuses text holding any other C0 control character (U+0000 to U+001F) withDICOM_JSON_MALFORMED_VALUE, under every Specific Character Set, so no escape sequence written as JSON text can turn into other characters when it is read back.toDicomJsonwrites such a character as it reads it, so JSON it wrote from a value holding one is refused when it comes back tofromDicomJson. - Text that reads back as other text.
fromDicomJsonreads back every text value it wrote and refuses withDICOM_JSON_UNSUPPORTED_CHARACTER_SETone that differs from the JSON's, such as a C1 code point underISO_IR 100, a character past U+007F under the default repertoire, a~underISO_IR 13, where0x7Eis OVERLINE, or a lone surrogate underISO_IR 192. - Odd groups below
0008, andFFFF. PS3.5 section 7.8.1 says elements in group0001,0003,0005,0007andFFFF"shall not be used", sofromDicomJsonrefuses a tag in one withDICOM_JSON_INVALID_JSON. FLandFDnegative zero. JSON writes-0as0, which is another IEEE value, sotoDicomJsonrefuses anFLorFDnegative zero withDICOM_JSON_UNREPRESENTABLE_NUMBER.- Nesting.
parseDicomreads Sequences nested at most 64 deep (NESTING_DEPTH_LIMIT), sofromDicomJsonrefuses JSON nested deeper withDICOM_JSON_NESTING_TOO_DEEPbefore it writes the next level: neither the reader's error nor a stack overflow leaves it. - No other error. Any error a converter meets that no code above names, one a callback threw
included, is refused with
DICOM_JSON_CONVERSION_FAILED. The error it replaces is not kept, since its message could hold a value; aresolveBulkDatathat cannot resolve a URI should returnundefined, which is refused withDICOM_JSON_BULK_DATA_UNRESOLVED.
A DS past 2^53 comes back as the same text:
import { fromDicomJson, parseDicom, serializeDicom, toDicomJson } from "@cosyte/dicom";
// Synthetic value: Patient's Weight (0010,1030) of 2^53 + 1, which no double holds.
const fileMeta = { transferSyntaxUID: "1.2.840.10008.1.2.1" };
const ds = fromDicomJson({ "00101030": { vr: "DS", Value: ["9007199254740993"] } }, { fileMeta });
const json = toDicomJson(parseDicom(serializeDicom(ds)));
json["00101030"]; // => { vr: "DS", Value: ["9007199254740993"] }
Where the bytes can differ. The JSON Model does not record everything Part 10 does, so the
round trip is byte-identical only where the source already wrote what fromDicomJson writes:
- Sequence and Item lengths. The model does not say whether a Sequence or Item had a defined
or an undefined length.
fromDicomJsonwrites defined lengths, or undefined lengths withsequenceLength: "undefined"; a source that mixed the two comes back with one of them. DSandIStext. ADSorISgiven as a JSON string is written as that text, so it keeps its form; read back bytoDicomJson, it comes out as a number. A number is written in JavaScript's shortest text for it, so1.0comes back as1,+5as5and-0as0; where that text is past the 16 charactersDSallows, the value is written as its significant digits and a power of ten, so100E14comes back as1e16. The decimal value never changes; aDSwhose value a JSON number does not hold is written as its text, as above.- Padding. Values are padded as PS3.5 pads them,
UIwith a NUL and other text with a space, so aUIa source padded with a space comes back padded with a NUL. Trailing spaces of a value are removed in the JSON. - An odd-length binary value from JSON. A Part 10 value has an even length, so an
InlineBinaryor resolvedBulkDataURIof an odd number of bytes is written with one NUL pad byte after it. Read back, that byte is part of the value:{ vr: "OB", InlineBinary: "AAAA" }(three bytes) comes back as"AAAAAA=="(four). JSON to Part 10 to JSON adds the byte; Part 10 to JSON to Part 10 never meets an odd value. - A
UNvalue read as a Sequence comes back asSQ, and a DICOMDIR's Directory Record offsets name bytes of the file it was read from, soserializeDicomrefuses the rebuilt Data Set withDICOM_SERIALIZE_DIRECTORY_OFFSET_UNRESOLVED.
What is refused, with a DicomJsonError. The converters refuse rather than change or guess a
value, and the message carries no value. DICOM_JSON_UNSUPPORTED_VR: SV and UV with a Value. A JSON
number holds an integer exactly only up to 2^53, and these VRs hold integers up to 2^63 and 2^64,
so we refuse them until we have read the form the annex gives them in a copy of PS3.18 pinned
here, rather than write one we guessed. fromDicomJson takes them by BulkDataURI, as bytes.
DICOM_JSON_BULK_DATA_URI_REQUIRED: a bulk value with no URI and no inlineBulkData. VR_MISMATCH: an
attribute whose VR the written Data Set does not read back with.
DICOM_JSON_UNSUPPORTED_ELEMENT: encapsulated Pixel Data with no URI from bulkDataUri, and any other
undefined-length value that is not a Sequence. DICOM_JSON_UNSUPPORTED_BYTE_ORDER: OW, OF, OD, OL
and OV under Explicit VR Big Endian, as an InlineBinary or from toDicomJson.
DICOM_JSON_UNREPRESENTABLE_NUMBER: NaN, an infinity, an FL or FD negative zero, a DS or IS that is
not a number, an IS whose value no JSON number holds exactly, or a number its VR cannot hold.
DICOM_JSON_MALFORMED_VALUE: a binary length that is not a whole number of values, a DS or IS string,
given or fetched, that is not one or is longer than its VR allows, a Person Name of more than three groups, text bytes the Specific Character Set does not decode, or a control character
the VR does not allow. DICOM_JSON_UNSUPPORTED_CHARACTER_SET: a Specific Character Set of more than one value
or one not carried, at any depth, or text fromDicomJson cannot encode, which is any character
outside 7-bit ASCII under a set other than the default repertoire, ISO_IR 100 and ISO_IR 192.
DICOM_JSON_UNSUPPORTED_TRANSFER_SYNTAX: fromDicomJson writes only the three uncompressed syntaxes and
Deflated Explicit VR LE. DICOM_JSON_NESTING_TOO_DEEP: Sequences nested more than 64 deep.
DICOM_JSON_INVALID_OPTION: a fallbackCharacterSet that is not a term of FALLBACK_CHARACTER_SETS.
DICOM_JSON_CONVERSION_FAILED: any other error. DICOM_JSON_INVALID_JSON (another shape, a delimiter inside one
value, an InlineBinary on a VR that is not binary, or a BulkDataURI on a VR the annex does not
allow one on), DICOM_JSON_BULK_DATA_UNRESOLVED and DICOM_JSON_VALUE_TOO_LONG cover the rest.
What the annex settles, and what it leaves open. We read PS3.18 Annex F of the 2026d
edition section by section. It settles the attribute order, DS and IS as JSON strings, the
VRs a BulkDataURI may stand on, the null for an empty value among several and an Item written
as {}, and the converters follow it there. It leaves the byte order of an InlineBinary open,
which is why that refusal stays. PS3.18 is not yet vendored in this repository, so this page cites
the annex by name and not clause by clause. Still our reading, not the annex's: the JSON form of
SV and UV values, which we refuse, which values count as bulk, and the rounding of an FL
number to float32. A JSON array of several Data Sets is not read; pass each Data Set in turn.
Where to go next
- The steps, with a sample that runs: Re-serialize a parsed object.
- What the parser tolerated on the way in: Tolerance and warnings.
- Stripping identifying metadata before you share the bytes: De-identification.