CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
json_writer_helper.h
1#pragma once
2
3#include <system/span.h>
4#include <system/scope_guard.h>
5#include <system/multicast_delegate.h>
6#include <system/memory_extensions.h>
7#include <system/details/stack_array.h>
8#include <system/buffers/array_pool.h>
9#include <system/array.h>
10#include <system/buffers/standard_format.h>
11#include <system/buffers/operation_status.h>
12#include <cstdint>
13
14#include "system/text/json/json_constants.h"
15
16namespace System
17{
18namespace Buffers
19{
20template <typename> class SearchValues;
21} // namespace Buffers
22class DateTime;
23class DateTimeOffset;
24namespace Text
25{
26namespace Encodings
27{
28namespace Web
29{
30class JavaScriptEncoder;
31} // namespace Web
32} // namespace Encodings
33} // namespace Text
34} // namespace System
35
36namespace System {
37
38namespace Text {
39
40namespace Json {
41
43{
45
46public:
47
48 template<typename T> using WriteCallback = T (*)(System::ReadOnlySpan<uint8_t>);
49
50public:
51
52 static const int32_t LastAsciiCharacter = 0x7F;
53
54 static void WriteIndentation(Span<uint8_t> buffer, int32_t indent, uint8_t indentByte);
55 static void ValidateNewLine(String value);
56 static void ValidateIndentCharacter(char16_t value);
57 static void ValidateIndentSize(int32_t value);
58 static void ValidateProperty(ReadOnlySpan<uint8_t> propertyName);
60 static void ValidateDouble(double value);
61 static void ValidateSingle(float value);
62 static void ValidateProperty(ReadOnlySpan<char16_t> propertyName);
70 static void ValidateNumber(ReadOnlySpan<uint8_t> utf8FormattedNumber);
71 static Buffers::OperationStatus ToUtf8(ReadOnlySpan<char16_t> source, Span<uint8_t> destination, int32_t& written);
72 template<typename T>
74 {
75 int32_t firstByteToEscape = System::Text::Json::JsonWriterHelper::NeedsEscaping(utf8Value, nullptr);
76
77 if (firstByteToEscape == -1)
78 {
79 int32_t quotedLength = utf8Value.get_Length() + 2;
81
82 {
83 auto __finally_guard_1 = ::System::MakeScopeGuard([&rented]()
84 {
85 if (!(rented == nullptr))
86 {
88 }
89 });
90
91 try
92 {
93 System::Details::StackArray<uint8_t, System::Text::Json::JsonConstants::StackallocByteThreshold> array_0;
94 Span<uint8_t> quotedValue = quotedLength > System::Text::Json::JsonConstants::StackallocByteThreshold ? MemoryExtensions::AsSpan((rented = Buffers::ArrayPool<uint8_t>::get_Shared()->Rent(quotedLength)), 0, quotedLength) : Span<uint8_t>(array_0).Slice(0, quotedLength);
95
97 utf8Value.CopyTo(quotedValue.Slice(1));
98 quotedValue[quotedValue.get_Length() - 1] = System::Text::Json::JsonConstants::Quote;
99
100 return writeCallback(quotedValue);
101 }
102 catch (...)
103 {
104 throw;
105 }
106 }
107 }
108 else
109 {
110
111 int32_t length = 2 + System::Text::Json::JsonWriterHelper::GetMaxEscapedLength(utf8Value.get_Length(), firstByteToEscape);
113
114 {
115 auto __finally_guard_0 = ::System::MakeScopeGuard([&rented]()
116 {
117 if (!(rented == nullptr))
118 {
120 }
121 });
122
123 try
124 {
125 Span<uint8_t> escapedValue;
126
128 {
129 rented = Buffers::ArrayPool<uint8_t>::get_Shared()->Rent(length);
130 escapedValue = Span<uint8_t>::to_Span(rented);
131 }
132 else
133 {
134 System::Details::StackArray<uint8_t, System::Text::Json::JsonConstants::StackallocByteThreshold> array_1;
135 escapedValue = array_1;
136 }
137
139 int32_t written;
140 System::Text::Json::JsonWriterHelper::EscapeString(utf8Value, escapedValue.Slice(1), firstByteToEscape, nullptr, written);
141 escapedValue[1 + written] = System::Text::Json::JsonConstants::Quote;
142
143 return writeCallback(escapedValue.Slice(0, written + 2));
144 }
145 catch (...)
146 {
147 throw;
148 }
149 }
150 }
151 }
152
153 static void WriteDateTimeTrimmed(Span<uint8_t> buffer, DateTime value, int32_t& bytesWritten);
154 static void WriteDateTimeOffsetTrimmed(Span<uint8_t> buffer, DateTimeOffset value, int32_t& bytesWritten);
155 static void TrimDateTimeOffset(Span<uint8_t> buffer, int32_t& bytesWritten);
158 static int32_t GetMaxEscapedLength(int32_t textLength, int32_t firstIndexToEscape);
159 static void EscapeString(ReadOnlySpan<uint8_t> value, Span<uint8_t> destination, int32_t indexOfFirstByteToEscape, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& written);
160 static void EscapeString(ReadOnlySpan<uint8_t> value, Span<uint8_t> destination, int32_t indexOfFirstByteToEscape, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& consumed, int32_t& written, bool isFinalBlock = true);
161 static void EscapeString(ReadOnlySpan<char16_t> value, Span<char16_t> destination, int32_t indexOfFirstByteToEscape, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& written);
162 static void EscapeString(ReadOnlySpan<char16_t> value, Span<char16_t> destination, int32_t indexOfFirstByteToEscape, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& consumed, int32_t& written, bool isFinalBlock = true);
163
164private:
165
166 static Buffers::StandardFormat s_dateTimeStandardFormat;
167
168 static ReadOnlySpan<uint8_t> get_AllowList();
169
170 static const String HexFormatString;
171 static Buffers::StandardFormat s_hexStandardFormat;
172
173 static bool NeedsEscaping(uint8_t value);
174 static bool NeedsEscapingNoBoundsCheck(char16_t value);
175 static void EscapeString(ReadOnlySpan<uint8_t> value, Span<uint8_t> destination, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& consumed, int32_t& written, bool isFinalBlock);
176 static void EscapeNextBytes(uint8_t value, Span<uint8_t> destination, int32_t& totalWritten);
177 static bool IsAsciiValue(uint8_t value);
178 static bool IsAsciiValue(char16_t value);
179 static void EscapeString(ReadOnlySpan<char16_t> value, Span<char16_t> destination, System::SharedPtr<Encodings::Web::JavaScriptEncoder> encoder, int32_t& consumed, int32_t& written, bool isFinalBlock);
180 static void EscapeNextChars(char16_t value, Span<char16_t> destination, int32_t& totalWritten);
181
182 static struct __StaticConstructor__ { __StaticConstructor__(); } s_constructor__;
183
184public:
186};
187
188} // namespace Json
189} // namespace Text
190} // namespace System
Provides a thread-safe pool for reusing managed array instances (ArrayPtr<T>).
Definition: array_pool.h:21
static SharedPtr< ArrayPool< T > > get_Shared()
Retrieves the process-wide shared pool instance. The instance is created lazily on first access and c...
Definition: array_pool.h:53
Represents a standard format specifier with an optional precision. It wraps a format symbol (a single...
Definition: standard_format.h:20
Represents a specific date and time value on the time continuum. This type should be allocated on sta...
Definition: date_time.h:50
Contains the date and time of day relative to Coordinated Universal Time. Objects of this class shoul...
Definition: date_time_offset.h:16
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Represents a contiguous region of arbitrary memory similar to C++20's std::span.
Definition: span.h:375
static ThisType to_Span(const typename BaseType::ArrayPtrT &array)
Converts an array to a Span.
Definition: span.h:402
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:126
static const uint8_t Quote
Definition: json_constants.h:33
static const int32_t StackallocByteThreshold
Definition: json_constants.h:107
Definition: json_writer_helper.h:43
static void ValidateNewLine(String value)
static void ValidateDouble(double value)
static void WriteDateTimeTrimmed(Span< uint8_t > buffer, DateTime value, int32_t &bytesWritten)
static void ValidateIndentSize(int32_t value)
T(*)(System::ReadOnlySpan< uint8_t >) WriteCallback
Definition: json_writer_helper.h:48
static void ValidateIndentCharacter(char16_t value)
static int32_t NeedsEscaping(ReadOnlySpan< uint8_t > value, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder)
static void ValidatePropertyNameLength(ReadOnlySpan< uint8_t > propertyName)
static void ValidatePropertyNameLength(ReadOnlySpan< char16_t > propertyName)
static void EscapeString(ReadOnlySpan< char16_t > value, Span< char16_t > destination, int32_t indexOfFirstByteToEscape, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder, int32_t &written)
static void EscapeString(ReadOnlySpan< uint8_t > value, Span< uint8_t > destination, int32_t indexOfFirstByteToEscape, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder, int32_t &written)
static void EscapeString(ReadOnlySpan< char16_t > value, Span< char16_t > destination, int32_t indexOfFirstByteToEscape, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder, int32_t &consumed, int32_t &written, bool isFinalBlock=true)
static void ValidatePropertyAndValue(ReadOnlySpan< uint8_t > propertyName, ReadOnlySpan< uint8_t > value)
static void ValidatePropertyAndValue(ReadOnlySpan< char16_t > propertyName, ReadOnlySpan< uint8_t > value)
static void ValidateProperty(ReadOnlySpan< uint8_t > propertyName)
static void ValidatePropertyAndValue(ReadOnlySpan< uint8_t > propertyName, ReadOnlySpan< char16_t > value)
static void EscapeString(ReadOnlySpan< uint8_t > value, Span< uint8_t > destination, int32_t indexOfFirstByteToEscape, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder, int32_t &consumed, int32_t &written, bool isFinalBlock=true)
static void WriteDateTimeOffsetTrimmed(Span< uint8_t > buffer, DateTimeOffset value, int32_t &bytesWritten)
static void WriteIndentation(Span< uint8_t > buffer, int32_t indent, uint8_t indentByte)
static T WriteString(ReadOnlySpan< uint8_t > utf8Value, JsonWriterHelper::WriteCallback< T > writeCallback)
Definition: json_writer_helper.h:73
static void ValidateSingle(float value)
static int32_t GetMaxEscapedLength(int32_t textLength, int32_t firstIndexToEscape)
static int32_t NeedsEscaping(ReadOnlySpan< char16_t > value, System::SharedPtr< Encodings::Web::JavaScriptEncoder > encoder)
static void ValidatePropertyAndValue(ReadOnlySpan< char16_t > propertyName, ReadOnlySpan< char16_t > value)
static void ValidateValue(ReadOnlySpan< char16_t > value)
static void ValidateNumber(ReadOnlySpan< uint8_t > utf8FormattedNumber)
static Buffers::OperationStatus ToUtf8(ReadOnlySpan< char16_t > source, Span< uint8_t > destination, int32_t &written)
static void ValidateProperty(ReadOnlySpan< char16_t > propertyName)
static void ValidateValue(ReadOnlySpan< uint8_t > value)
static const int32_t LastAsciiCharacter
Definition: json_writer_helper.h:52
static void TrimDateTimeOffset(Span< uint8_t > buffer, int32_t &bytesWritten)
OperationStatus
Represents the status of an operation that processes a buffer.
Definition: operation_status.h:11
@ Text
Defines color adjustment information for text.
Span< T > AsSpan(const ArrayPtr< T > &array, int32_t start=0, int32_t length=-1)
Creates a span from an array.
Definition: memory_extensions.h:2051
Definition: db_command.h:9