8#ifndef INCLUDED_BALJSN_ENCODER
9#define INCLUDED_BALJSN_ENCODER
173#include <balscm_version.h>
198#include <bsl_cstddef.h>
199#include <bsl_iostream.h>
200#include <bsl_ostream.h>
201#include <bsl_sstream.h>
202#include <bsl_streambuf.h>
203#include <bsl_string.h>
204#include <bsl_string_view.h>
205#include <bsl_vector.h>
236 bsl::ostream& logStream();
258 template <
class TYPE>
259 int encode(bsl::streambuf *streamBuf,
262 template <
class TYPE>
263 int encode(bsl::streambuf *streamBuf,
274 template <
class TYPE>
275 int encode(bsl::ostream& stream,
278 template <
class TYPE>
279 int encode(bsl::ostream& stream,
289 template <
class TYPE>
290 int encode(bsl::streambuf *streamBuf,
const TYPE& value);
299 template <
class TYPE>
300 int encode(bsl::ostream& stream,
const TYPE& value);
364 template <
class TYPE>
365 static int encode(bsl::ostream *jsonStream,
382 template <
class TYPE>
383 static int encode(bsl::ostream *logStream,
384 bsl::ostream *jsonStream,
407 template <
class TYPE>
408 static int encode(
bool *isValueEmpty,
410 bsl::ostream *logStream,
430 template <
class TYPE>
431 static int validate(bsl::ostream *logStream,
434 template <
class TYPE,
class CATEGORY>
435 static int validate(bsl::ostream *logStream,
445 template <
class TYPE>
446 static int validateChoice(bsl::ostream *logStream,
const TYPE& value);
468 template <
class TYPE>
513 template <
class TYPE>
515 bsl::ostream *logStream,
541 bsl::ostream *logStream,
548 template <
class TYPE>
551 bsl::ostream *logStream,
558 template <
class TYPE,
class OTHER_CATEGORY>
561 bsl::ostream *logStream,
567 OTHER_CATEGORY category);
570 bsl::ostream *logStream,
574 bsl::ostream *logStream,
592 bsl::ostream *logStream,
636 bsl::ostream *d_logStream_p;
648 bool d_isNextObjectFirst;
657 bsl::ostream *logStream,
685 template <
class TYPE>
687 template <
class TYPE>
689 template <
class TYPE>
692 template <
class TYPE>
695 template <
class TYPE>
698 template <
class TYPE>
701 template <
class TYPE>
703 template <
class TYPE>
707 template <
class TYPE>
738 bsl::ostream *d_logStream_p;
747 bool d_isNextElementFirst;
756 bsl::ostream *logStream,
780 template <
class TYPE>
814 bsl::ostream *d_logStream_p;
823 bool d_isNextElementFirst;
832 bsl::ostream *logStream,
859 template <
class TYPE>
861 template <
class TYPE>
863 template <
class TYPE>
866 template <
class TYPE>
869 template <
class TYPE>
872 template <
class TYPE>
875 template <
class TYPE>
877 template <
class TYPE>
881 template <
class TYPE>
925 bsl::ostream *d_logStream_p;
930 bool d_isNextObjectFirst;
943 bsl::ostream *logStream,
969 template <
class TYPE,
class SELECTION_INFO>
970 int operator()(
const TYPE& selection,
const SELECTION_INFO& selectionInfo);
1015 bsl::ostream *d_logStream_p;
1030 bool d_isNextObjectFirst;
1038 bsl::ostream *logStream,
1068 template <
class TYPE>
1071 template <
class TYPE>
1074 template <
class TYPE,
class CATEGORY>
1075 int operator()(
const TYPE& selection, CATEGORY category);
1078 template <
class TYPE>
1122 bsl::ostream *d_logStream_p;
1127 bool d_isNextAttributeFirst;
1140 bsl::ostream *logStream,
1166 template <
class TYPE,
class ATTRIBUTE_INFO>
1167 int operator()(
const TYPE& attribute,
const ATTRIBUTE_INFO& attributeInfo);
1212 bsl::ostream *d_logStream_p;
1227 bool d_isNextAttributeFirst;
1236 bsl::ostream *logStream,
1266 template <
class TYPE>
1268 template <
class TYPE>
1271 template <
class TYPE>
1274 template <
class TYPE>
1277 template <
class TYPE,
class CATEGORY>
1278 int operator()(
const TYPE& attribute, CATEGORY category);
1281 template <
class TYPE>
1301bsl::ostream& Encoder::logStream()
1309: d_logStream(basicAllocator)
1314template <
class TYPE>
1319 return encode(streamBuf, value, options);
1322template <
class TYPE>
1327 return encode(stream, value, options);
1330template <
class TYPE>
1337 d_logStream.clear();
1338 d_logStream.
str(
"");
1346 <<
"Encoded object must be a Sequence, Choice, or Array type."
1351 bsl::ostream outputStream(streamBuf);
1359 streamBuf->pubsync();
1365 if (!outputStream) {
1367 <<
"An error occurred when writing to the supplied output stream"
1368 " or stream buffer."
1370 streamBuf->pubsync();
1374 streamBuf->pubsync();
1379template <
class TYPE>
1385 return encode(streamBuf, value, options ? *options : localOpts);
1388template <
class TYPE>
1393 if (!stream.good()) {
1394 logStream() <<
"Invalid stream." << bsl::endl;
1398 const int rc = this->
encode(stream.rdbuf(), value, options);
1400 stream.setstate(bsl::ios_base::failbit);
1407template <
class TYPE>
1414 return encode(stream, value, options ? *options : localOpts);
1421 return d_logStream.
str();
1445 bsl::ostream *outputStream,
1449 (*outputStream) <<
'\n';
1455template <
class TYPE>
1462 bsl::ostream logStream(&logStreamBuf);
1464 return encode(&logStream, jsonStream, value, options);
1467template <
class TYPE>
1470 bsl::ostream *jsonStream,
1475 static const bool s_FIRST_MEMBER_FLAG =
false;
1483 bool isValueEmpty =
false;
1485 int rc =
encode(&isValueEmpty,
1491 s_FIRST_MEMBER_FLAG);
1494 *logStream <<
"Encoding failed leaving an unclosed element (rc = "
1501template <
class TYPE>
1504 bsl::ostream *logStream,
1517 int rc = visitor(value, Category());
1528template <
class TYPE>
1536template <
class TYPE,
class CATEGORY>
1543template <
class TYPE>
1549 (*logStream) <<
"Undefined selection for Choice object" << bsl::endl;
1558template <
class TYPE>
1564 return formatter->
putValue(value, &options);
1575 *isPrefixEmpty =
true;
1581 *isPrefixEmpty =
false;
1590 *isSuffixEmpty =
true;
1596 *isSuffixEmpty =
false;
1608template <
class TYPE>
1611 bsl::ostream *logStream,
1622 for (
int index = 0; index != size; ++index) {
1636template <
class TYPE>
1647 bool *isMemberEmpty,
1649 bsl::ostream *logStream,
1674 *isMemberEmpty =
false;
1680 (*logStream) <<
"Unable to encode value of element "
1681 <<
"named: '" << memberName <<
"'." << bsl::endl;
1685 *isMemberEmpty =
false;
1689template <
class TYPE,
class OTHER_CATEGORY>
1691 bool *isMemberEmpty,
1693 bsl::ostream *logStream,
1699 OTHER_CATEGORY category)
1706 bool isPrefixEmpty =
false;
1717 bool isValueEmpty =
false;
1724 !isPrefixEmpty || isFirstMember);
1726 (*logStream) <<
"Unable to encode value of element "
1727 <<
"named: '" << memberName <<
"'." << bsl::endl;
1736 *isMemberEmpty = isFirstMember && isValueEmpty;
1743 bsl::ostream *logStream,
1747 if (!isFirstMember) {
1753 (*logStream) <<
"Unable to encode element name: '" << memberName
1754 <<
"'." << bsl::endl;
1764 bsl::ostream *logStream,
1781 bool *isPrefixEmpty,
1783 bsl::ostream *logStream,
1789 *isPrefixEmpty =
true;
1801 *isPrefixEmpty =
false;
1813 bsl::ostream *logStream,
1815 bool isNextObjectFirst,
1817: d_formatter_p(formatter)
1818, d_logStream_p(logStream)
1819, d_options_p(&options)
1820, d_formattingMode(formattingMode)
1821, d_isNextObjectFirst(isNextObjectFirst)
1830 d_isNextObjectFirst =
false;
1836template <
class TYPE>
1844 d_isNextObjectFirst =
true;
1850 d_isNextObjectFirst =
false;
1862 d_isNextObjectFirst =
false;
1866template <
class TYPE>
1876 bool isPrefixEmpty =
false;
1883 !isPrefixEmpty || d_isNextObjectFirst,
1892 bool isSuffixEmpty =
false;
1897 d_isNextObjectFirst = isPrefixEmpty && isSelectionEmpty && isSuffixEmpty;
1901template <
class TYPE>
1908 &d_isNextObjectFirst,
1914 d_isNextObjectFirst);
1917template <
class TYPE>
1925template <
class TYPE>
1933 d_isNextObjectFirst =
false;
1939template <
class TYPE>
1947 d_isNextObjectFirst =
false;
1954 d_isNextObjectFirst,
1966template <
class TYPE>
1971 bool isPrefixEmpty =
false;
1978 !isPrefixEmpty || d_isNextObjectFirst,
1988 bool isSuffixEmpty =
false;
1993 d_isNextObjectFirst = isPrefixEmpty && isAttributeEmpty && isSuffixEmpty;
1997template <
class TYPE>
2002 d_isNextObjectFirst =
false;
2008template <
class TYPE>
2020 return d_isNextObjectFirst;
2031 bsl::ostream *logStream,
2032 bool isNextElementFirst,
2034: d_formatter_p(formatter)
2035, d_logStream_p(logStream)
2036, d_options_p(&options)
2037, d_isNextElementFirst(isNextElementFirst)
2042template <
class TYPE>
2048 d_isNextElementFirst,
2052 int rc = dispatcher(element, Category());
2065 return d_isNextElementFirst;
2076 bsl::ostream *logStream,
2077 bool isNextElementFirst,
2079: d_formatter_p(formatter)
2080, d_logStream_p(logStream)
2081, d_options_p(&options)
2082, d_isNextElementFirst(isNextElementFirst)
2091 if (!d_isNextElementFirst) {
2102 d_isNextElementFirst =
false;
2106template <
class TYPE>
2116 if (!d_isNextElementFirst) {
2122 d_isNextElementFirst =
false;
2134 d_isNextElementFirst =
false;
2138template <
class TYPE>
2147 if (!d_isNextElementFirst) {
2164 d_isNextElementFirst =
false;
2168template <
class TYPE>
2170 const TYPE& element,
2178template <
class TYPE>
2180 const TYPE& element,
2186template <
class TYPE>
2188 const TYPE& element,
2191 if (!d_isNextElementFirst) {
2198 d_isNextElementFirst =
false;
2204template <
class TYPE>
2206 const TYPE& element,
2211 if (elementIsNull) {
2212 if (!d_isNextElementFirst) {
2217 d_isNextElementFirst =
false;
2223 d_isNextElementFirst,
2235template <
class TYPE>
2239 if (!d_isNextElementFirst) {
2257 d_isNextElementFirst =
false;
2261template <
class TYPE>
2265 if (!d_isNextElementFirst) {
2269 d_isNextElementFirst =
false;
2275template <
class TYPE>
2286 return d_isNextElementFirst;
2297 bsl::ostream *logStream,
2298 bool isNextObjectFirst,
2300: d_formatter_p(formatter)
2301, d_logStream_p(logStream)
2302, d_isNextObjectFirst(isNextObjectFirst)
2303, d_options_p(&options)
2308template <
class TYPE,
class SELECTION_INFO>
2311 const SELECTION_INFO& selectionInfo)
2315 selectionInfo.name(),
2316 selectionInfo.formattingMode(),
2317 d_isNextObjectFirst,
2321 int rc = dispatcher(selection, Category());
2335 return d_isNextObjectFirst;
2346 bsl::ostream *logStream,
2349 bool isNextObjectFirst,
2351: d_formatter_p(formatter)
2352, d_logStream_p(logStream)
2353, d_options_p(&options)
2354, d_selectionName(selectionName)
2355, d_formattingMode(formattingMode)
2356, d_isNextObjectFirst(isNextObjectFirst)
2361template <
class TYPE>
2364 const TYPE& selection,
2372template <
class TYPE>
2375 const TYPE& selection,
2381template <
class TYPE,
class CATEGORY>
2389 d_selectionName.
data(),
2393 d_isNextObjectFirst,
2397template <
class TYPE>
2409 return d_isNextObjectFirst;
2420 bsl::ostream *logStream,
2421 bool isNextAttributeFirst,
2423: d_formatter_p(formatter)
2424, d_logStream_p(logStream)
2425, d_isNextAttributeFirst(isNextAttributeFirst)
2426, d_options_p(&options)
2431template <
class TYPE,
class ATTRIBUTE_INFO>
2433 const ATTRIBUTE_INFO& attributeInfo)
2437 attributeInfo.name(),
2438 attributeInfo.formattingMode(),
2439 d_isNextAttributeFirst,
2443 int rc = dispatcher(attribute, Category());
2456 return d_isNextAttributeFirst;
2467 bsl::ostream *logStream,
2470 bool isNextAttributeFirst,
2472: d_formatter_p(formatter)
2473, d_logStream_p(logStream)
2474, d_options_p(&options)
2475, d_attributeName(attributeName)
2476, d_formattingMode(formattingMode)
2477, d_isNextAttributeFirst(isNextAttributeFirst)
2490 d_attributeName.
data(),
2494 d_isNextAttributeFirst,
2498template <
class TYPE>
2501 const TYPE& attribute,
2513 d_attributeName.
data(),
2517 d_isNextAttributeFirst,
2521template <
class TYPE>
2524 const TYPE& attribute,
2532template <
class TYPE>
2535 const TYPE& attribute,
2541template <
class TYPE>
2544 const TYPE& attribute,
2555 d_attributeName.
data(),
2559 d_isNextAttributeFirst,
2563template <
class TYPE,
class CATEGORY>
2571 d_attributeName.
data(),
2575 d_isNextAttributeFirst,
2579template <
class TYPE>
2591 return d_isNextAttributeFirst;
2616 bsl::ostream& d_outputStream;
2617 bool d_usePrettyStyle;
2619 int d_spacesPerLevel;
2620 bool d_isArrayElement;
2710 return d_isArrayElement;
Definition baljsn_encoderoptions.h:262
@ e_PRETTY
Definition baljsn_encoderoptions.h:316
bool encodeNullElements() const
Definition baljsn_encoderoptions.h:941
baljsn::EncoderOptions::EncodingStyle encodingStyle() const
Definition baljsn_encoderoptions.h:929
int initialIndentLevel() const
Definition baljsn_encoderoptions.h:917
int spacesPerLevel() const
Definition baljsn_encoderoptions.h:923
bool encodeEmptyArrays() const
Definition baljsn_encoderoptions.h:935
Definition baljsn_encoder.h:1191
int FormattingMode
Definition baljsn_encoder.h:1202
bool isNextAttributeFirst() const
Definition baljsn_encoder.h:2589
int operator()(const bsl::vector< char > &attribute, bdlat_TypeCategory::Array category)
Definition baljsn_encoder.h:2483
Encoder_AttributeDispatcher(Formatter *formatter, bsl::ostream *logStream, const bsl::string_view &attributeName, FormattingMode formattingMode, bool isNextAttributeFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2465
Definition baljsn_encoder.h:1101
int operator()(const TYPE &attribute, const ATTRIBUTE_INFO &attributeInfo)
Definition baljsn_encoder.h:2432
int FormattingMode
Definition baljsn_encoder.h:1112
Encoder_AttributeVisitor(Formatter *formatter, bsl::ostream *logStream, bool isNextAttributeFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2418
bool isNextAttributeFirst() const
Definition baljsn_encoder.h:2454
Definition baljsn_encoder.h:805
Encoder_ElementDispatcher(Formatter *formatter, bsl::ostream *logStream, bool isNextElementFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2074
int operator()(const bsl::vector< char > &element, bdlat_TypeCategory::Array category)
Definition baljsn_encoder.h:2088
bool isNextElementFirst() const
Definition baljsn_encoder.h:2284
Definition baljsn_encoder.h:729
bool isNextElementFirst() const
Definition baljsn_encoder.h:2063
Encoder_ElementVisitor(Formatter *formatter, bsl::ostream *logStream, bool isNextElementFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2029
int operator()(const TYPE &element)
Definition baljsn_encoder.h:2044
Definition baljsn_encoder.h:994
int operator()(const TYPE &selection, bdlat_TypeCategory::CustomizedType category)
Definition baljsn_encoder.h:2363
Encoder_SelectionDispatcher(Formatter *formatter, bsl::ostream *logStream, const bsl::string_view &selectionName, FormattingMode formattingMode, bool isNextObjectFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2344
bool isNextObjectFirst() const
Definition baljsn_encoder.h:2407
int FormattingMode
Definition baljsn_encoder.h:1005
Definition baljsn_encoder.h:904
int FormattingMode
Definition baljsn_encoder.h:915
bool isNextObjectFirst() const
Definition baljsn_encoder.h:2333
int operator()(const TYPE &selection, const SELECTION_INFO &selectionInfo)
Definition baljsn_encoder.h:2310
Encoder_SelectionVisitor(Formatter *formatter, bsl::ostream *logStream, bool isNextObjectFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:2295
Definition baljsn_encoder.h:615
Encoder_ValueDispatcher(Formatter *formatter, bsl::ostream *logStream, FormattingMode formattingMode, bool isNextObjectFirst, const EncoderOptions &options)
Definition baljsn_encoder.h:1811
bool isNextObjectFirst() const
Definition baljsn_encoder.h:2018
int FormattingMode
Definition baljsn_encoder.h:626
int operator()(const bsl::vector< char > &value, bdlat_TypeCategory::Array category)
Definition baljsn_encoder.h:1827
Definition baljsn_encoder.h:225
int encode(bsl::streambuf *streamBuf, const TYPE &value, const EncoderOptions &options)
Definition baljsn_encoder.h:1331
~Encoder()=default
Destroy this object.
bsl::string loggedMessages() const
Definition baljsn_encoder.h:1419
Definition bdlsb_memoutstreambuf.h:212
Definition bslstl_ostringstream.h:175
void str(const StringType &value)
Definition bslstl_ostringstream.h:581
Definition bslstl_stringview.h:441
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1760
Definition bslstl_string.h:1281
Definition bslstl_vector.h:1025
Definition bslma_allocator.h:457
static int accessByCategory(const TYPE &object, ACCESSOR &accessor)
Definition bdlat_typecategory.h:1444
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:1856
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition baljsn_datumdecoderoptions.h:113
bsl::size_t size(const TYPE &array)
Return the number of elements in the specified array.
int accessElement(const TYPE &array, ACCESSOR &accessor, int index)
int accessSelection(const TYPE &object, ACCESSOR &accessor)
@ k_UNDEFINED_SELECTION_ID
Definition bdlat_choicefunctions.h:511
int selectionId(const TYPE &object)
const BaseType< TYPE >::Type & convertToBaseType(const TYPE &object)
void toString(bsl::string *result, const TYPE &value)
bool isNull(const TYPE &object)
int accessValue(const TYPE &object, ACCESSOR &accessor)
int accessAttributes(const TYPE &object, ACCESSOR &accessor)
bdlat_TypeCategory::Value select(const TYPE &object)
Definition baljsn_encoder.h:316
Encoder_EncodeImplUtil ThisUtil
ThisUtil is a convenience alias for this utility struct.
Definition baljsn_encoder.h:321
int FormattingMode
Definition baljsn_encoder.h:329
static void openDocument(bsl::ostream *outputStream, const EncoderOptions &options)
Definition baljsn_encoder.h:1433
static int validateChoice(bsl::ostream *logStream, const TYPE &value)
Definition baljsn_encoder.h:1544
static int encodeMember(bool *isMemberEmpty, Formatter *formatter, bsl::ostream *logStream, const bsl::string_view &memberName, const bsl::vector< char > &member, FormattingMode formattingMode, const EncoderOptions &options, bool isFirstMember, bdlat_TypeCategory::Array category)
static void encodeObjectPrefix(bool *isPrefixEmpty, Formatter *formatter, FormattingMode formattingMode)
Definition baljsn_encoder.h:1570
static int encode(bsl::ostream *jsonStream, const TYPE &value, const EncoderOptions &options=EncoderOptions())
Definition baljsn_encoder.h:1457
static int encodeSimpleValue(Formatter *formatter, const TYPE &value, const EncoderOptions &options)
Definition baljsn_encoder.h:1560
static void encodeObjectSuffix(bool *isSuffixEmpty, Formatter *formatter, FormattingMode formattingMode)
Definition baljsn_encoder.h:1585
static int encodeCharArray(Formatter *formatter, const bsl::vector< char > &value, const EncoderOptions &options)
static int encodeNonEmptyArray(Formatter *formatter, bsl::ostream *logStream, const TYPE &value, const EncoderOptions &options)
Definition baljsn_encoder.h:1609
static int encodeMemberPrefix(Formatter *formatter, bsl::ostream *logStream, const bsl::string_view &memberName, bool isFirstMember)
Definition baljsn_encoder.h:1741
static int validate(bsl::ostream *logStream, const TYPE &value, bdlat_TypeCategory::Choice category)
Definition baljsn_encoder.h:1529
static void closeDocument(bsl::ostream *outputStream, const EncoderOptions &options)
Definition baljsn_encoder.h:1444
static void encodeEmptyArray(Formatter *formatter)
Definition baljsn_encoder.h:1602
static int accessValueByCategory(const TYPE &object, ACCESSOR &accessor)
Definition bdlat_nullablevalueutil.h:323
Definition bdlat_typecategory.h:1035
Definition bdlat_typecategory.h:1036
Definition bdlat_typecategory.h:1037
Definition bdlat_typecategory.h:1034
Definition bdlat_typecategory.h:1038
Definition bdlat_typecategory.h:1039
Definition bdlat_typecategory.h:1040
Definition bdlat_typecategory.h:1041
Value
Definition bdlat_typecategory.h:1044
@ e_ARRAY_CATEGORY
Definition bdlat_typecategory.h:1046
@ e_CHOICE_CATEGORY
Definition bdlat_typecategory.h:1047
@ e_SEQUENCE_CATEGORY
Definition bdlat_typecategory.h:1051
static bsl::ostream & indent(bsl::ostream &stream, int level, int spacesPerLevel=4)
This struct is empty and represents a nil type.
Definition bslmf_nil.h:131