Skip to content

Commit de310f6

Browse files
authored
Fix associative container on json values (#20606)
1 parent 1850c07 commit de310f6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

modules/openapi-generator/src/main/resources/cpp-ue4/helpers-header.mustache

+31
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ inline void WriteJsonValue(JsonWriter& Writer, const TMap<FString, T>& Value)
298298
Writer->WriteObjectEnd();
299299
}
300300

301+
template <typename T>
302+
inline void WriteJsonValue(JsonWriter& Writer, const TSet<T>& Value)
303+
{
304+
Writer->WriteArrayStart();
305+
for (const auto& Element : Value)
306+
{
307+
WriteJsonValue(Writer, Element);
308+
}
309+
Writer->WriteArrayEnd();
310+
}
311+
301312
//////////////////////////////////////////////////////////////////////////
302313

303314
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, FString& Value)
@@ -465,6 +476,26 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FSt
465476
return true;
466477
}
467478

479+
template <typename T>
480+
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TSet<T>& ArrayValue)
481+
{
482+
const TArray<TSharedPtr<FJsonValue>>* JsonArray;
483+
if (JsonValue->TryGetArray(JsonArray))
484+
{
485+
bool ParseSuccess = true;
486+
const int32 Count = JsonArray->Num();
487+
ArrayValue.Reset();
488+
for (int i = 0; i < Count; i++)
489+
{
490+
T TmpValue;
491+
ParseSuccess &= TryGetJsonValue((*JsonArray)[i], TmpValue);
492+
ArrayValue.Emplace(MoveTemp(TmpValue));
493+
}
494+
return ParseSuccess;
495+
}
496+
return false;
497+
}
498+
468499
{{#cppNamespaceDeclarations}}
469500
}
470501
{{/cppNamespaceDeclarations}}

samples/client/petstore/cpp-ue4/Public/OpenAPIHelpers.h

+31
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ inline void WriteJsonValue(JsonWriter& Writer, const TMap<FString, T>& Value)
307307
Writer->WriteObjectEnd();
308308
}
309309

310+
template <typename T>
311+
inline void WriteJsonValue(JsonWriter& Writer, const TSet<T>& Value)
312+
{
313+
Writer->WriteArrayStart();
314+
for (const auto& Element : Value)
315+
{
316+
WriteJsonValue(Writer, Element);
317+
}
318+
Writer->WriteArrayEnd();
319+
}
320+
310321
//////////////////////////////////////////////////////////////////////////
311322

312323
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, FString& Value)
@@ -474,4 +485,24 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FSt
474485
return true;
475486
}
476487

488+
template <typename T>
489+
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TSet<T>& ArrayValue)
490+
{
491+
const TArray<TSharedPtr<FJsonValue>>* JsonArray;
492+
if (JsonValue->TryGetArray(JsonArray))
493+
{
494+
bool ParseSuccess = true;
495+
const int32 Count = JsonArray->Num();
496+
ArrayValue.Reset();
497+
for (int i = 0; i < Count; i++)
498+
{
499+
T TmpValue;
500+
ParseSuccess &= TryGetJsonValue((*JsonArray)[i], TmpValue);
501+
ArrayValue.Emplace(MoveTemp(TmpValue));
502+
}
503+
return ParseSuccess;
504+
}
505+
return false;
506+
}
507+
477508
}

0 commit comments

Comments
 (0)