UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JsonObjectArrayUpdater.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Dom/JsonObject.h"
7#include "Dom/JsonValue.h"
8
19template <typename ElementType, typename KeyType>
21{
22 DECLARE_DELEGATE_RetVal_OneParam(KeyType, FGetElementKey, const ElementType&);
23
25
27
29
31 {
32 if (SourceArray.Num() > 0)
33 {
35 {
38 {
39 // Build a map of elements for quick access and to keep track of which ones got updated
41 for (const ElementType& Element : SourceArray)
42 {
43 ElementsMap.Add(GetElementKey.Execute(Element), &Element);
44 }
45
46 // Update existing json values and discard entries that no longer exist or are invalid
48 {
51 {
52 KeyType ElementKey;
54 {
55 if (const ElementType** ElementPtr = ElementsMap.Find(ElementKey))
56 {
59 ElementsMap.Remove(ElementKey);
60 }
61 }
62 }
63 }
64
65 // Add new elements
66 for (auto It = ElementsMap.CreateConstIterator(); It; ++It)
67 {
69 UpdateJsonObject.Execute(*It.Value(), *NewJsonObject.Get());
71 }
72 }
73 else
74 {
75 // Array doesn't exist in the given JsonObject, so build a new array
76 for (const ElementType& Element : SourceArray)
77 {
79 UpdateJsonObject.Execute(Element, *NewJsonObject.Get());
81 }
82 }
83 }
84
85 // Optionally sort new json array before setting incase the user doesn't want the default
86 // appending of new items at the end.
87 SortJsonArray.ExecuteIfBound(NewJsonValues);
88
89 // Set the new content of the json array
91 }
92 else
93 {
94 // Source array is empty so remove the json array
95 JsonObject.RemoveField(ArrayName);
96 }
97 }
98};
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition JsonObject.h:23
JSON_API void RemoveField(FStringView FieldName)
Definition JsonObject.cpp:15
JSON_API bool TryGetArrayField(FStringView FieldName, const TArray< TSharedPtr< FJsonValue > > *&OutArray) const
Definition JsonObject.cpp:208
JSON_API void SetArrayField(FString &&FieldName, TArray< TSharedPtr< FJsonValue > > &&Array)
Definition JsonObject.cpp:214
Definition JsonValue.h:294
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition JsonObjectArrayUpdater.h:21
DECLARE_DELEGATE_TwoParams(FUpdateJsonObject, const ElementType &, FJsonObject &)
DECLARE_DELEGATE_OneParam(FSortArray, TArray< TSharedPtr< FJsonValue > > &)
DECLARE_DELEGATE_RetVal_OneParam(KeyType, FGetElementKey, const ElementType &)
DECLARE_DELEGATE_RetVal_TwoParams(bool, FTryGetJsonObjectKey, const FJsonObject &, KeyType &)
static void Execute(FJsonObject &JsonObject, const FString &ArrayName, const TArray< ElementType > &SourceArray, FGetElementKey GetElementKey, FTryGetJsonObjectKey TryGetJsonObjectKey, FUpdateJsonObject UpdateJsonObject, FSortArray SortJsonArray=FSortArray())
Definition JsonObjectArrayUpdater.h:30