/usr/include/dcmtk/dcmdata/dcjson.h is in libdcmtk-dev 3.6.2-3build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | /*
*
* Copyright (C) 2017, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: dcmdata
*
* Author: Sebastian Grallert
*
* Purpose: Providing basic JSON formatting functionalities
*
*/
#ifndef DCJSON_H
#define DCJSON_H
#include "dcmtk/config/osconfig.h" // make sure OS specific configuration is included first
#include "dcmtk/ofstd/ofdefine.h"
#include "dcmtk/ofstd/ofstring.h"
#include "dcmtk/dcmdata/dctagkey.h"
/** Class for handling JSON format options.
* Base class to implement custom formatting.
* Purpose:
* - individual output formatting
* - normalization of DecimalString and IntegerString e.g.\ normalization of leading zeros
* - escaping of special JSON control characters
* - outputting the correct indention and symbols for "Value", "BulkDataURI", etc.
*
* <h3>Usage Example:</h3>
* @code{.cpp}
#include "dcmtk/dcmdata/dcjson.h"
// ...
DcmFileFormat fileformat;
if(fileformat.loadFile("test.dcm").good())
{
// print the DICOM file in JSON format
// using the pretty format (muti-line with indention and other whitespace)
fileformat.writeJson(COUT, DcmJsonFormatPretty(OFTrue));
// using the compact (single line, without unneeded whitespace) format
fileformat.writeJson(COUT, DcmJsonFormatCompact(OFTrue));
}
@endcode
* <h3>Implementing a custom formatter:</h3>
* @code{.cpp}
struct CustomJsonFormat : DcmJsonFormatPretty
{
CustomJsonFormat(const OFBool printMetaInfo = OFTrue)
: DcmJsonFormatPretty(printMetaInfo)
{
}
OFString OFJsonFormatExample::space()
{
// use tabstops instead of spaces for indention
return "\t";
}
}
@endcode
*/
class DCMTK_DCMDATA_EXPORT DcmJsonFormat
{
public:
/** A class to create small proxy objects that ease indention handling.
* Each Indention object only contains a reference to the DcmJsonFormat object
* that created it and its only purpose is to call the respective methods
* of that object when one of its overloaded operators is used.
*/
class Indention
{
public:
/** output current indention to an output stream.
* @param out the output stream to use
* @param indention the indention to print
* @return out
*/
friend inline STD_NAMESPACE ostream& operator<<(STD_NAMESPACE ostream& out, const Indention& indention)
{
indention.printIndention(out);
return out;
}
/** increases current indention.
* @return *this
*/
inline Indention& operator++()
{
m_Format.increaseIndention();
return *this;
}
/** decreases current indention
* @return *this
*/
inline Indention& operator--()
{
m_Format.decreaseIndention();
return *this;
}
private:
/// allow DcmJsonFormat to use this class' private members
friend class DcmJsonFormat;
/// private constructor, used by DcmJsonFormat
inline Indention(DcmJsonFormat& format) : m_Format(format)
{
}
/// prints the current indention using the parent formatter
inline void printIndention(STD_NAMESPACE ostream& out) const
{
m_Format.printIndention(out);
}
/// reference to the parent formatter object
DcmJsonFormat& m_Format;
};
/** Escapes all forbidden control characters in JSON
* @param out output stream to which the escaped String is written
* @param value String that should be escaped
*/
static void escapeControlCharacters(STD_NAMESPACE ostream &out, OFString const &value);
/** Normalize Decimal String to specific JSON format.
* remove leading zeros, except before dot.
* @b Example:
* @code{.txt}
00.123 --> 0.123
023.12 --> 23.12
-01.00 --> -1.00
0200 --> 200
.12 --> 0.12
000.1 --> 0.1
@endcode
* @param value String that should be normalize
*/
static void normalizeDecimalString(OFString &value);
/** Normalize Integer String to specific JSON format.
* remove leading zeros, except before dot.
* @b Example:
* @code{.txt}
000 --> 0
023 --> 23
-01 --> -1
0200 --> 200
@endcode
* @param value String that should be normalize
*/
static void normalizeIntegerString(OFString &value);
/** Prints either null if empty or the string value
* (with all illegal characters escaped).
* @param out output stream to which the Value prefix is written
* @param value String that should be printed
*/
static void printString(STD_NAMESPACE ostream &out,
const OFString &value);
/** Prints either null if empty or a quoted string
* (with leading and ending quotation marks and all
* illegal characters escaped).
* @param out output stream to which the Value prefix is written
* @param value String that should be printed
*/
static void printValueString(STD_NAMESPACE ostream &out,
const OFString &value);
/** Print either null if empty or a Number as normelized IntegerString
* @param out output stream to which the Value prefix is written
* @param value String that should be printed
*/
static void printNumberInteger(STD_NAMESPACE ostream &out,
OFString &value);
/** Print either null if empty or a Number as normelized IntegerDecimal
* @param out output stream to which the Value prefix is written
* @param value String that should be printed
*/
static void printNumberDecimal(STD_NAMESPACE ostream &out,
OFString &value);
/** Constructor
* @param printMetaInfo parameter that defines if meta information should be written
*/
inline DcmJsonFormat(const OFBool printMetaInfo)
: printMetaheaderInformation(printMetaInfo)
{
}
/** Method to return line break(s)
* @return line break(s).
*/
virtual OFString newline() = 0;
/** Method to return whitespace(s)
* @return whitespace(s).
*/
virtual OFString space() = 0;
/** Method to return an indention proxy object for increasing, decreasing or printing indention
* @return an indention proxy object.
*/
inline Indention indent()
{
return Indention(*this);
}
/** Check if an attribute should be exported as BulkDataURI.
* Override this function to implement bulk data URI output.
* @param tag the tag of the attribute being printed, for letting
* the implementation decide how to handle it.
* @param uri the resulting URI to output.
* @return OFTrue if yes, OFFalse if no.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
struct BulkDataURIJsonFormat : DcmJsonFormatPretty
{
CustomJsonFormat(const OFBool printMetaInfo = OFTrue,
... bulkDataURIDatabase)
: DcmJsonFormatPretty(printMetaInfo)
, TheDatabase(bulkDataURIDatabase)
{
}
virtual OFBool asBulkDataURI(const DcmTagKey& tag, OFString& uri)
{
... result = TheDatabase.findBulkDataFor(tag);
if (result.found())
{
uri = result.uri();
return OFTrue;
}
return OFFalse;
}
... TheDatabase;
}
@endcode
*/
virtual OFBool asBulkDataURI(const DcmTagKey& tag, OFString& uri);
/** Print the Prefix which for JSON Values needed
* with indention and newlines as in the format Variable given.
* @b Example:
* @code{.txt}
,"Value":[
@endcode
* @param out output stream to which the Value prefix is written
*/
virtual void printValuePrefix(STD_NAMESPACE ostream &out);
/** Print the Suffix which for JSON Values needed
* with indention and newlines as in the format Variable given.
* @b Example:
* @code{.txt}
]\n
@endcode
* @param out output stream to which the Value prefix is written
*/
virtual void printValueSuffix(STD_NAMESPACE ostream &out);
/** Print the Prefix which for JSON BulkDataURI needed
* with indention and newlines as in the format Variable given.
* @b Example:
* @code{.txt}
,"BulkDataURI":
@endcode
* @param out output stream to which the Value prefix is written
*/
virtual void printBulkDataURIPrefix(STD_NAMESPACE ostream &out);
/** Print the Prefix which for JSON InlineBinary needed
* with indention and newlines as the format specifies.
* @b Example:
* @code{.txt}
,"InlineBinary":
@endcode
* @param out output stream to which the Value prefix is written
*/
virtual void printInlineBinaryPrefix(STD_NAMESPACE ostream &out);
/** Print the prefix for array elements (except the first one), with
* indention and newlines as the format specifies.
* @b Example:
* @code{.txt}
Example,\n
Example...
@endcode
* @param out output stream to which the Value prefix is written
*/
virtual void printNextArrayElementPrefix(STD_NAMESPACE ostream &out);
/** Option that defines if metaheader information should be printed.
*/
const OFBool printMetaheaderInformation;
protected:
/** Indent to the specific level.
* @param out output stream to which the indention is written.
*/
virtual void printIndention(STD_NAMESPACE ostream& out) = 0;
/** Used for increasing the indention level.
*/
virtual void increaseIndention() = 0;
/** Used for decreasing the indention level.
*/
virtual void decreaseIndention() = 0;
};
/** Subclass for handling JSON formatted output.
* Standard class for formatted output.
*/
class DCMTK_DCMDATA_EXPORT DcmJsonFormatPretty : public DcmJsonFormat
{
private:
/** Variable for the indentenlevel of DcmJsonFormat
*/
unsigned m_IndentionLevel;
public:
/** DcmJsonFormatPretty constructor
* @param printMetaInfo Enable/Disable including Metaheader in the output
*/
explicit DcmJsonFormatPretty(const OFBool printMetaInfo = OFTrue);
/** Indent to the specific level.
* @param out output stream to which the indention is written.
*/
void printIndention(STD_NAMESPACE ostream& out);
/** Increase the indention level.
*/
void increaseIndention();
/** Decrease the indention level.
*/
void decreaseIndention();
/** Print a newline
* @returns a newline
*/
OFString newline();
/** Print a space
* @returns a space
*/
OFString space();
};
/** Subclass for handling JSON formatted output.
* Standard class for non-formatted output.
*/
class DCMTK_DCMDATA_EXPORT DcmJsonFormatCompact : public DcmJsonFormat
{
public:
/** DcmJsonFormatCompact constructor
* @param printMetaInfo Enable/Disable including Metaheader in the output
*/
explicit DcmJsonFormatCompact(const OFBool printMetaInfo = OFTrue);
/** Does nothing.
* @param out output stream to which the indention is written.
*/
void printIndention(STD_NAMESPACE ostream& out);
/** Does nothing.
*/
void increaseIndention();
/** Does nothing.
*/
void decreaseIndention();
/** Does nothing.
* @returns a empty String.
*/
OFString newline();
/** Does nothing.
* @returns a empty String.
*/
OFString space();
};
#endif /* DCJSON_H */
|