Skip to content

Commit 3acb3b9

Browse files
committed
Merge branch 'development' of https://git01.codeplex.com/forks/blakedgross/casablanca into json_exception_free
2 parents 34b5d38 + 63b7b11 commit 3acb3b9

File tree

5 files changed

+432
-87
lines changed

5 files changed

+432
-87
lines changed

Release/include/cpprest/json.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,14 @@ namespace json
391391
/// <param name="value">The C++ value to create a JSON value from, a C++ STL double-byte string</param>
392392
_ASYNCRTIMP static value parse(const utility::string_t&);
393393

394+
/// <summary>
395+
/// Attempts to parse a string and construct a JSON value.
396+
/// </summary>
397+
/// <param name="value">The C++ value to create a JSON value from, a C++ STL double-byte string</param>
398+
/// <param name="errorCode">If parsing fails, the error code is greater than 0</param>
399+
/// <returns>The parsed object. Returns web::json::value::null if failed</returns>
400+
_ASYNCRTIMP static value parse(const utility::string_t&, std::error_code&);
401+
394402
/// <summary>
395403
/// Serializes the current JSON value to a C++ string.
396404
/// </summary>
@@ -411,6 +419,14 @@ namespace json
411419
/// <returns>The JSON value object created from the input stream.</returns>
412420
_ASYNCRTIMP static value parse(utility::istream_t &input);
413421

422+
/// <summary>
423+
/// Parses a JSON value from the contents of an input stream using the native platform character width.
424+
/// </summary>
425+
/// <param name="input">The stream to read the JSON value from</param>
426+
/// <param name="errorCode">If parsing fails, the error code is greater than 0</param>
427+
/// <returns>The parsed object. Returns web::json::value::null if failed</returns>
428+
_ASYNCRTIMP static value parse(utility::istream_t &input, std::error_code& error);
429+
414430
/// <summary>
415431
/// Writes the current JSON value to a stream with the native platform character width.
416432
/// </summary>
@@ -424,6 +440,14 @@ namespace json
424440
/// <param name="stream">The stream to read the JSON value from</param>
425441
_ASYNCRTIMP static value parse(std::istream& stream);
426442

443+
/// <summary>
444+
/// Parses a JSON value from the contents of a single-byte (UTF8) stream.
445+
/// </summary>
446+
/// <param name="stream">The stream to read the JSON value from</param>
447+
/// <param name="errorCode">If parsing fails, the error code is greater than 0</param>
448+
/// <returns>The parsed object. Returns web::json::value::null if failed</returns>
449+
_ASYNCRTIMP static value parse(std::istream& stream, std::error_code& error);
450+
427451
/// <summary>
428452
/// Serializes the content of the value into a single-byte (UTF8) stream.
429453
/// </summary>
@@ -634,6 +658,70 @@ namespace json
634658
~json_exception() CPPREST_NOEXCEPT {}
635659
};
636660

661+
namespace details
662+
{
663+
enum json_error
664+
{
665+
left_over_character_in_stream = 1,
666+
malformed_array_literal,
667+
malformed_comment,
668+
malformed_literal,
669+
malformed_object_literal,
670+
malformed_numeric_literal,
671+
malformed_string_literal,
672+
malformed_token,
673+
mismatched_brances,
674+
nesting,
675+
unexpected_token
676+
};
677+
678+
class json_error_category_impl : public std::error_category
679+
{
680+
public:
681+
virtual const char* json_error_category_impl::name() const
682+
{
683+
return "json";
684+
}
685+
686+
virtual std::string json_error_category_impl::message(int ev) const
687+
{
688+
switch (ev)
689+
{
690+
case json_error::left_over_character_in_stream:
691+
return "Left-over characters in stream after parsing a JSON value";
692+
case json_error::malformed_array_literal:
693+
return "Malformed array literal";
694+
case json_error::malformed_comment:
695+
return "Malformed comment";
696+
case json_error::malformed_literal:
697+
return "Malformed literal";
698+
case json_error::malformed_object_literal:
699+
return "Malformed object literal";
700+
case json_error::malformed_numeric_literal:
701+
return "Malformed numeric literal";
702+
case json_error::malformed_string_literal:
703+
return "Malformed string literal";
704+
case json_error::malformed_token:
705+
return "Malformed token";
706+
case json_error::mismatched_brances:
707+
return "Mismatched braces";
708+
case json_error::nesting:
709+
return "Nesting too deep";
710+
case json_error::unexpected_token:
711+
return "Unexpected token";
712+
default:
713+
return "Unknown json error";
714+
}
715+
}
716+
};
717+
718+
inline const json_error_category_impl& json_error_category()
719+
{
720+
static json_error_category_impl instance;
721+
return instance;
722+
}
723+
}
724+
637725
/// <summary>
638726
/// A JSON array represented as a C++ class.
639727
/// </summary>

0 commit comments

Comments
 (0)