MatureJsonList.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // Copyright 2024 Tracer Interactive, LLC. All Rights Reserved.
  2. #include "MatureJsonList.h"
  3. #include "MatureJsonEnums.h"
  4. #include "MatureJsonValue.h"
  5. #include "MatureJsonObject.h"
  6. //#include "MatureJsonHelpers.h"
  7. #include "Policies/CondensedJsonPrintPolicy.h"
  8. #include "Policies/PrettyJsonPrintPolicy.h"
  9. FMatureJsonList::ListWrap::ListWrap()
  10. : Document(MakeShared<mature::Document>())
  11. , List(Document->SetArray().GetArray())
  12. {
  13. }
  14. FMatureJsonList::ListWrap::ListWrap(TSharedPtr<mature::Document> Doc, const mature::Array& Ref)
  15. : Document(Doc)
  16. , List(Ref)
  17. {
  18. }
  19. FMatureJsonList::ListWrap::ListWrap(TSharedPtr<mature::Document> Doc, const mature::Array&& Ref)
  20. : Document(Doc)
  21. , List(Ref)
  22. {
  23. }
  24. FMatureJsonList::FMatureJsonList()
  25. : ListPtr(MakeShared<ListWrap>())
  26. {
  27. }
  28. FMatureJsonList::FMatureJsonList(const TSharedPtr<mature::Document>& Doc,const mature::Array& a)
  29. : ListPtr(MakeShared<ListWrap>(Doc, a))
  30. {
  31. }
  32. FMatureJsonList::FMatureJsonList(const TSharedPtr<mature::Document>& Doc, const mature::Array&& a)
  33. : ListPtr(MakeShared<ListWrap>(Doc, a))
  34. {
  35. }
  36. FMatureJsonList::FMatureJsonList(const FMatureJsonList& rhs)
  37. : ListPtr(rhs.ListPtr)
  38. {
  39. }
  40. int32 FMatureJsonList::Size() const
  41. {
  42. return List().Size();
  43. }
  44. bool FMatureJsonList::IsEmpty() const
  45. {
  46. return List().Empty();
  47. }
  48. void FMatureJsonList::Clear()
  49. {
  50. List().Clear();
  51. }
  52. FMatureJsonList& FMatureJsonList::Append(const FMatureJsonList& List)
  53. {
  54. auto it = List.List().Begin();
  55. for (; it != List.List().End(); it++) {
  56. AddValue().SetValue(*it);
  57. }
  58. return *this;
  59. }
  60. FMatureJsonList& FMatureJsonList::Append(const TArray<FMatureJsonValue>& Value) {
  61. for (auto& it : Value) {
  62. AddValue().SetValue(it);
  63. }
  64. return *this;
  65. }
  66. FMatureJsonList& FMatureJsonList::AddValue( bool Value )
  67. {
  68. AddValue().SetValue(Value);
  69. return *this;
  70. }
  71. FMatureJsonList& FMatureJsonList::AddValue( float Value )
  72. {
  73. AddValue().SetValue(Value);
  74. return *this;
  75. }
  76. FMatureJsonList& FMatureJsonList::AddValue( int32 Value )
  77. {
  78. AddValue().SetValue(Value);
  79. return *this;
  80. }
  81. FMatureJsonList& FMatureJsonList::AddValue( double Value )
  82. {
  83. AddValue().SetValue(Value);
  84. return *this;
  85. }
  86. FMatureJsonList& FMatureJsonList::AddValue(const TCHAR* Value)
  87. {
  88. AddValue().SetValue(Value);
  89. return *this;
  90. }
  91. FMatureJsonList& FMatureJsonList::AddValue( const FString& Value )
  92. {
  93. AddValue().SetValue(Value);
  94. return *this;
  95. }
  96. FMatureJsonList& FMatureJsonList::AddValue( const FDateTime& Value )
  97. {
  98. AddValue().SetValue(Value);
  99. return *this;
  100. }
  101. FMatureJsonList& FMatureJsonList::AddValue( const FGuid& Value )
  102. {
  103. AddValue().SetValue(Value);
  104. return *this;
  105. }
  106. FMatureJsonList& FMatureJsonList::AddValue( const FColor& Value )
  107. {
  108. AddValue().SetValue(Value);
  109. return *this;
  110. }
  111. FMatureJsonList& FMatureJsonList::AddValue(const FLinearColor& Value)
  112. {
  113. AddValue().SetValue(Value);
  114. return *this;
  115. }
  116. FMatureJsonList& FMatureJsonList::AddValue(const FRotator& Value)
  117. {
  118. AddValue().SetValue(Value);
  119. return *this;
  120. }
  121. FMatureJsonList& FMatureJsonList::AddValue(const FTransform& Value)
  122. {
  123. AddValue().SetValue(Value);
  124. return *this;
  125. }
  126. FMatureJsonList& FMatureJsonList::AddValue(const FVector& Value)
  127. {
  128. AddValue().SetValue(Value);
  129. return *this;
  130. }
  131. FMatureJsonList& FMatureJsonList::AddValue( const FMatureJsonValue& Value )
  132. {
  133. AddValue().SetValue(Value);
  134. return *this;
  135. }
  136. FMatureJsonList& FMatureJsonList::AddValue(const FMatureJsonObject& Value) {
  137. AddValue().SetValue(Value);
  138. return *this;
  139. }
  140. FMatureJsonList& FMatureJsonList::AddValue( const FMatureJsonList& Value)
  141. {
  142. auto NewList = AddValue().ToList();
  143. for (auto& it : Value.List()) {
  144. NewList.List().PushBack(it, GetAllocator());
  145. }
  146. return *this;
  147. }
  148. FMatureJsonList& FMatureJsonList::AddValue( const TArray<FMatureJsonValue>& Value )
  149. {
  150. auto NewList = AddValue().ToList();
  151. for (auto& it : Value) {
  152. NewList.List().PushBack(it.ValueRef(), GetAllocator());
  153. }
  154. return *this;
  155. }
  156. // Set an item as a boolean.
  157. FMatureJsonList& FMatureJsonList::AddValue(const TArray<bool>& Value) {
  158. for (auto& it : Value) {
  159. AddValue(it);
  160. }
  161. return *this;
  162. }
  163. // Set an item as a float.
  164. FMatureJsonList& FMatureJsonList::AddValue(const TArray<float>& Value) {
  165. for (auto& it : Value) {
  166. AddValue(it);
  167. }
  168. return *this;
  169. }
  170. // Set an item as an integer.
  171. FMatureJsonList& FMatureJsonList::AddValue(const TArray<int32>& Value) {
  172. for (auto& it : Value) {
  173. AddValue(it);
  174. }
  175. return *this;
  176. }
  177. // Set an item as a number.
  178. FMatureJsonList& FMatureJsonList::AddValue(const TArray<double>& Value) {
  179. for (auto& it : Value) {
  180. AddValue(it);
  181. }
  182. return *this;
  183. }
  184. // Set an item as a string.
  185. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FString>& Value) {
  186. for (auto& it : Value) {
  187. AddValue(it);
  188. }
  189. return *this;
  190. }
  191. // Set an item as a date/time.
  192. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FDateTime>& Value) {
  193. for (auto& it : Value) {
  194. AddValue(it);
  195. }
  196. return *this;
  197. }
  198. // Set an item as a GUID.
  199. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FGuid>& Value) {
  200. for (auto& it : Value) {
  201. AddValue(it);
  202. }
  203. return *this;
  204. }
  205. // Set an item as a color.
  206. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FColor>& Value) {
  207. for (auto& it : Value) {
  208. AddValue(it);
  209. }
  210. return *this;
  211. }
  212. // Set an item as a linear color.
  213. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FLinearColor>& Value) {
  214. for (auto& it : Value) {
  215. AddValue(it);
  216. }
  217. return *this;
  218. }
  219. // Set an item as a rotator.
  220. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FRotator>& Value) {
  221. for (auto& it : Value) {
  222. AddValue(it);
  223. }
  224. return *this;
  225. }
  226. // Set an item as a transform.
  227. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FTransform>& Value) {
  228. for (auto& it : Value) {
  229. AddValue(it);
  230. }
  231. return *this;
  232. }
  233. // Set an item as a vector.
  234. FMatureJsonList& FMatureJsonList::AddValue(const TArray<FVector>& Value) {
  235. for (auto& it : Value) {
  236. AddValue(it);
  237. }
  238. return *this;
  239. }
  240. FMatureJsonValue FMatureJsonList::AddValue()const {
  241. const int idx = List().Size();
  242. List().PushBack(mature::Value(), GetAllocator());
  243. return FMatureJsonValue(Document(), List()[idx]);
  244. }
  245. FMatureJsonObject FMatureJsonList::AddObject() {
  246. const int idx = List().Size();
  247. List().PushBack(mature::Value(), GetAllocator());
  248. return FMatureJsonValue(Document(), List()[idx]).ToObject();
  249. }
  250. FMatureJsonList FMatureJsonList::AddList() {
  251. const int idx = List().Size();
  252. List().PushBack(mature::Value(), GetAllocator());
  253. return FMatureJsonValue(Document(), List()[idx]).ToList();
  254. }
  255. // Set an item as a boolean.
  256. FMatureJsonList FMatureJsonList::AddList(const TArray<bool>& Value) {
  257. auto NewList = AddValue().ToList();
  258. for (auto& it : Value) {
  259. NewList.AddValue(it);
  260. }
  261. return NewList;
  262. }
  263. // Set an item as a float.
  264. FMatureJsonList FMatureJsonList::AddList(const TArray<float>& Value) {
  265. auto NewList = AddValue().ToList();
  266. for (auto& it : Value) {
  267. NewList.AddValue(it);
  268. }
  269. return NewList;
  270. }
  271. // Set an item as an integer.
  272. FMatureJsonList FMatureJsonList::AddList(const TArray<int32>& Value) {
  273. auto NewList = AddValue().ToList();
  274. for (auto& it : Value) {
  275. NewList.AddValue(it);
  276. }
  277. return NewList;
  278. }
  279. // Set an item as a number.
  280. FMatureJsonList FMatureJsonList::AddList(const TArray<double>& Value) {
  281. auto NewList = AddValue().ToList();
  282. for (auto& it : Value) {
  283. NewList.AddValue(it);
  284. }
  285. return NewList;
  286. }
  287. // Set an item as a string.
  288. FMatureJsonList FMatureJsonList::AddList(const TArray<FString>& Value) {
  289. auto NewList = AddValue().ToList();
  290. for (auto& it : Value) {
  291. NewList.AddValue(it);
  292. }
  293. return NewList;
  294. }
  295. // Set an item as a date/time.
  296. FMatureJsonList FMatureJsonList::AddList(const TArray<FDateTime>& Value) {
  297. auto NewList = AddValue().ToList();
  298. for (auto& it : Value) {
  299. NewList.AddValue(it);
  300. }
  301. return NewList;
  302. }
  303. // Set an item as a GUID.
  304. FMatureJsonList FMatureJsonList::AddList(const TArray<FGuid>& Value) {
  305. auto NewList = AddValue().ToList();
  306. for (auto& it : Value) {
  307. NewList.AddValue(it);
  308. }
  309. return NewList;
  310. }
  311. // Set an item as a color.
  312. FMatureJsonList FMatureJsonList::AddList(const TArray<FColor>& Value) {
  313. auto NewList = AddValue().ToList();
  314. for (auto& it : Value) {
  315. NewList.AddValue(it);
  316. }
  317. return NewList;
  318. }
  319. // Set an item as a linear color.
  320. FMatureJsonList FMatureJsonList::AddList(const TArray<FLinearColor>& Value) {
  321. auto NewList = AddValue().ToList();
  322. for (auto& it : Value) {
  323. NewList.AddValue(it);
  324. }
  325. return NewList;
  326. }
  327. // Set an item as a rotator.
  328. FMatureJsonList FMatureJsonList::AddList(const TArray<FRotator>& Value) {
  329. auto NewList = AddValue().ToList();
  330. for (auto& it : Value) {
  331. NewList.AddValue(it);
  332. }
  333. return NewList;
  334. }
  335. // Set an item as a transform.
  336. FMatureJsonList FMatureJsonList::AddList(const TArray<FTransform>& Value) {
  337. auto NewList = AddValue().ToList();
  338. for (auto& it : Value) {
  339. NewList.AddValue(it);
  340. }
  341. return NewList;
  342. }
  343. // Set an item as a vector.
  344. FMatureJsonList FMatureJsonList::AddList(const TArray<FVector>& Value) {
  345. auto NewList = AddValue().ToList();
  346. for (auto& it : Value) {
  347. NewList.AddValue(it);
  348. }
  349. return NewList;
  350. }
  351. // Set an item as a vector.
  352. FMatureJsonList& FMatureJsonList::MergeList(const FMatureJsonList& rhs) {
  353. int idx = 0;
  354. for (; idx < rhs.Size(); idx++) {
  355. AddValue(rhs.GetValue(idx));
  356. }
  357. return *this;
  358. }
  359. FMatureJsonList& FMatureJsonList::SetValue(int32 Index) {
  360. GetValue(Index).SetValue();
  361. return *this;
  362. }
  363. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, bool Value )
  364. {
  365. GetValue(Index).SetValue(Value);
  366. return *this;
  367. }
  368. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, float Value )
  369. {
  370. GetValue(Index).SetValue(Value);
  371. return *this;
  372. }
  373. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, int32 Value )
  374. {
  375. GetValue(Index).SetValue(Value);
  376. return *this;
  377. }
  378. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, double Value )
  379. {
  380. GetValue(Index).SetValue(Value);
  381. return *this;
  382. }
  383. FMatureJsonList& FMatureJsonList::SetValue(int32 Index, const TCHAR* Value) {
  384. GetValue(Index).SetValue(Value);
  385. return *this;
  386. }
  387. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FString& Value )
  388. {
  389. GetValue(Index).SetValue(Value);
  390. return *this;
  391. }
  392. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FDateTime& Value )
  393. {
  394. GetValue(Index).SetValue(Value);
  395. return *this;
  396. }
  397. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FGuid& Value )
  398. {
  399. GetValue(Index).SetValue(Value);
  400. return *this;
  401. }
  402. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FColor& Value )
  403. {
  404. GetValue(Index).SetValue(Value);
  405. return *this;
  406. }
  407. FMatureJsonList& FMatureJsonList::SetValue(int32 Index, const FLinearColor& Value)
  408. {
  409. GetValue(Index).SetValue(Value);
  410. return *this;
  411. }
  412. FMatureJsonList& FMatureJsonList::SetValue(int32 Index, const FRotator& Value)
  413. {
  414. GetValue(Index).SetValue(Value);
  415. return *this;
  416. }
  417. FMatureJsonList& FMatureJsonList::SetValue(int32 Index, const FTransform& Value)
  418. {
  419. GetValue(Index).SetValue(Value);
  420. return *this;
  421. }
  422. FMatureJsonList& FMatureJsonList::SetValue(int32 Index, const FVector& Value)
  423. {
  424. GetValue(Index).SetValue(Value);
  425. return *this;
  426. }
  427. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FMatureJsonValue& Value )
  428. {
  429. GetValue(Index).SetValue(Value);
  430. return *this;
  431. }
  432. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FMatureJsonObject& Value )
  433. {
  434. GetValue(Index).SetValue().SetValue(Value);
  435. return *this;
  436. }
  437. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const FMatureJsonList& Value )
  438. {
  439. GetValue(Index).SetValue().SetValue(Value);
  440. return *this;
  441. }
  442. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const TArray<FMatureJsonValue>& Value )
  443. {
  444. GetValue(Index).SetValue().ToList().AddValue(Value);
  445. return *this;
  446. }
  447. FMatureJsonList& FMatureJsonList::SetValue( int32 Index, const TMap<FString, FMatureJsonValue>& Value )
  448. {
  449. GetValue(Index).SetValue().ToObject().AddValue(Value);
  450. return *this;
  451. }
  452. FMatureJsonValue FMatureJsonList::GetValue(int Index)const {
  453. if (Index < 0)return FMatureJsonValue();
  454. if (Size() <= Index) {
  455. int l = Index;
  456. for (; Size() <= l; l--) {
  457. AddValue();
  458. }
  459. }
  460. return FMatureJsonValue(Document(), List()[Index]);
  461. }
  462. bool FMatureJsonList::GetValue(int32 Index, bool& Value)const
  463. {
  464. if (Size() <= Index) return false;
  465. GetValue(Index).GetValue(Value);
  466. return true;
  467. }
  468. bool FMatureJsonList::GetValue(int32 Index, float& Value)const
  469. {
  470. if (Size() <= Index) return false;
  471. GetValue(Index).GetValue(Value);
  472. return true;
  473. }
  474. bool FMatureJsonList::GetValue(int32 Index, int32& Value)const
  475. {
  476. if (Size() <= Index) return false;
  477. GetValue(Index).GetValue(Value);
  478. return true;
  479. }
  480. bool FMatureJsonList::GetValue(int32 Index, double& Value)const
  481. {
  482. if (Size() <= Index) return false;
  483. GetValue(Index).GetValue(Value);
  484. return true;
  485. }
  486. bool FMatureJsonList::GetValue(int32 Index, FString& Value)const
  487. {
  488. if (Size() <= Index) return false;
  489. GetValue(Index).GetValue(Value);
  490. return true;
  491. }
  492. bool FMatureJsonList::GetValue(int32 Index, FDateTime& Value)const
  493. {
  494. if (Size() <= Index) return false;
  495. GetValue(Index).GetValue(Value);
  496. return true;
  497. }
  498. bool FMatureJsonList::GetValue(int32 Index, FGuid& Value)const
  499. {
  500. if (Size() <= Index) return false;
  501. GetValue(Index).GetValue(Value);
  502. return true;
  503. }
  504. bool FMatureJsonList::GetValue(int32 Index, FColor& Value)const
  505. {
  506. if (Size() <= Index) return false;
  507. GetValue(Index).GetValue(Value);
  508. return true;
  509. }
  510. bool FMatureJsonList::GetValue(int32 Index, FLinearColor& Value)const
  511. {
  512. if (Size() <= Index) return false;
  513. GetValue(Index).GetValue(Value);
  514. return true;
  515. }
  516. bool FMatureJsonList::GetValue(int32 Index, FRotator& Value)const
  517. {
  518. if (Size() <= Index) return false;
  519. GetValue(Index).GetValue(Value);
  520. return true;
  521. }
  522. bool FMatureJsonList::GetValue(int32 Index, FTransform& Value)const
  523. {
  524. if (Size() <= Index) return false;
  525. GetValue(Index).GetValue(Value);
  526. return true;
  527. }
  528. bool FMatureJsonList::GetValue(int32 Index, FVector& Value)const
  529. {
  530. if (Size() <= Index) return false;
  531. GetValue(Index).GetValue(Value);
  532. return true;
  533. }
  534. bool FMatureJsonList::GetValue(int32 Index, FMatureJsonValue& Value)const
  535. {
  536. Value = GetValue(Index);
  537. return true;
  538. }
  539. bool FMatureJsonList::GetValue(int32 Index, FMatureJsonObject& Value)const
  540. {
  541. Value = GetValue(Index).ToObject(true);
  542. return true;
  543. }
  544. bool FMatureJsonList::GetValue(int32 Index, FMatureJsonList& Value)const
  545. {
  546. Value = GetValue(Index).ToList(true);
  547. return true;
  548. }
  549. FMatureJsonValue FMatureJsonList::ToValue( int32 Index) const {
  550. FMatureJsonValue value;
  551. GetValue(Index, value);
  552. return value;
  553. }
  554. FMatureJsonObject FMatureJsonList::ToObject( int32 Index) const {
  555. FMatureJsonObject value;
  556. GetValue(Index, value);
  557. return value;
  558. }
  559. FMatureJsonList FMatureJsonList::ToList( int32 Index) const {
  560. FMatureJsonList value;
  561. GetValue(Index, value);
  562. return value;
  563. }
  564. FMatureJsonList& FMatureJsonList::Remove(unsigned int Index, unsigned int num)
  565. {
  566. if (List().Size() <= Index || num==0)return *this;
  567. unsigned int idx = 0;
  568. mature::Array::ConstValueIterator begin = List().End();
  569. mature::Array::ConstValueIterator end = List().Begin();
  570. //mature::Array::ConstValueIterator it = List().Begin();
  571. for (; end != List().End(); idx++, end++) {
  572. if (idx == Index) {
  573. begin = end;
  574. }
  575. else if ((Index + num)<=idx) {
  576. break;
  577. }
  578. }
  579. List().Erase(begin,end);
  580. return *this;
  581. }
  582. FMatureJsonList& FMatureJsonList::operator=(const FMatureJsonList& rhs) {
  583. ListPtr = rhs.ListPtr;
  584. return *this;
  585. }
  586. TSharedPtr<mature::Document> FMatureJsonList::Document() {
  587. return ListPtr->Document;
  588. }
  589. TSharedPtr<mature::Document> FMatureJsonList::Document()const {
  590. return ListPtr->Document;
  591. }
  592. inline mature::Array& FMatureJsonList::List() {
  593. return ListPtr->List;
  594. }
  595. inline mature::Array& FMatureJsonList::List()const {
  596. return ListPtr->List;
  597. }
  598. inline mature::Allocator& FMatureJsonList::GetAllocator() {
  599. return ListPtr->Document->GetAllocator();
  600. }
  601. inline mature::Allocator& FMatureJsonList::GetAllocator()const {
  602. return ListPtr->Document->GetAllocator();
  603. }