Welcome to mirror list, hosted at ThFree Co, Russian Federation.

idprop_serialize.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8381b0a175569975218e5612164e3200116863c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2021 Blender Foundation. */

#include <optional>

#include "DNA_ID.h"

#include "BKE_idprop.hh"

#include "BLI_listbase.h"

namespace blender::bke::idprop {
using namespace blender::io::serialize;

/* Forward declarations */
class IDPropertySerializer;
struct DictionaryEntryParser;
static IDProperty *idprop_from_value(const DictionaryValue &value);
static const IDPropertySerializer &serializer_for(eIDPropertyType property_type);
static const IDPropertySerializer &serializer_for(StringRef idprop_typename);

/* -------------------------------------------------------------------- */
/** \name ID property serialization.
 * \{ */

/* Definitions */
static constexpr StringRef IDP_KEY_NAME("name");
static constexpr StringRef IDP_KEY_TYPE("type");
static constexpr StringRef IDP_KEY_SUBTYPE("subtype");
static constexpr StringRef IDP_KEY_VALUE("value");

static constexpr StringRef IDP_PROPERTY_TYPENAME_STRING("IDP_STRING");
static constexpr StringRef IDP_PROPERTY_TYPENAME_INT("IDP_INT");
static constexpr StringRef IDP_PROPERTY_TYPENAME_FLOAT("IDP_FLOAT");
static constexpr StringRef IDP_PROPERTY_TYPENAME_DOUBLE("IDP_DOUBLE");
static constexpr StringRef IDP_PROPERTY_TYPENAME_ARRAY("IDP_ARRAY");
static constexpr StringRef IDP_PROPERTY_TYPENAME_GROUP("IDP_GROUP");
static constexpr StringRef IDP_PROPERTY_TYPENAME_UNKNOWN("IDP_UNKNOWN");

/**
 * \brief Base class for (de)serializing IDProperties.
 *
 * Has a subclass for supported IDProperties and one for unsupported IDProperties.
 */
class IDPropertySerializer {
 public:
  constexpr IDPropertySerializer() = default;

  /**
   * \brief return the type name for (de)serializing.
   * Type name  is stored in the `type` or `subtype` attribute of the serialized id_property.
   */
  virtual std::string type_name() const = 0;

  /**
   * \brief return the IDPropertyType for (de)serializing.
   */
  virtual std::optional<eIDPropertyType> property_type() const = 0;

  /**
   * \brief create dictionary containing the given id_property.
   */
  virtual std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const = 0;

  /**
   * \brief convert the entry to an id property.
   */
  virtual std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const = 0;

  /**
   * \brief Can the serializer be used?
   *
   * IDP_ID and IDP_IDPARRAY aren't supported for serialization.
   */
  virtual bool supports_serializing() const
  {
    return true;
  }

 protected:
  /**
   * \brief Create a new DictionaryValue instance.
   *
   * Only fill the dictionary with common attributes (name, type).
   */
  std::shared_ptr<DictionaryValue> create_dictionary(const struct IDProperty *id_property) const
  {
    std::shared_ptr<DictionaryValue> result = std::make_shared<DictionaryValue>();
    DictionaryValue::Items &attributes = result->elements();
    attributes.append_as(std::pair(IDP_KEY_NAME, new StringValue(id_property->name)));
    attributes.append_as(std::pair(IDP_KEY_TYPE, new StringValue(type_name())));
    return result;
  }
};

/**
 * \brief Helper class for parsing DictionaryValues.
 */
struct DictionaryEntryParser {
  const DictionaryValue::Lookup lookup;

 public:
  explicit DictionaryEntryParser(const DictionaryValue &value) : lookup(value.create_lookup())
  {
  }

  std::optional<eIDPropertyType> get_type() const
  {
    return get_id_property_type(IDP_KEY_TYPE);
  }

  std::optional<eIDPropertyType> get_subtype() const
  {
    return get_id_property_type(IDP_KEY_SUBTYPE);
  }

  std::optional<std::string> get_name() const
  {
    return get_string(IDP_KEY_NAME);
  }

  std::optional<std::string> get_string_value() const
  {
    return get_string(IDP_KEY_VALUE);
  }

  std::optional<int32_t> get_int_value() const
  {
    return get_int(IDP_KEY_VALUE);
  }

  std::optional<float> get_float_value() const
  {
    return get_float(IDP_KEY_VALUE);
  }

  std::optional<double> get_double_value() const
  {
    return get_double(IDP_KEY_VALUE);
  }

  const ArrayValue *get_array_value() const
  {
    return get_array(IDP_KEY_VALUE);
  }

  std::optional<Vector<int32_t>> get_array_int_value() const
  {
    return get_array_primitive<int32_t, IntValue>(IDP_KEY_VALUE);
  }

  std::optional<Vector<float>> get_array_float_value() const
  {
    return get_array_primitive<float, DoubleValue>(IDP_KEY_VALUE);
  }

  std::optional<Vector<double>> get_array_double_value() const
  {
    return get_array_primitive<double, DoubleValue>(IDP_KEY_VALUE);
  }

 private:
  std::optional<std::string> get_string(StringRef key) const
  {
    const DictionaryValue::LookupValue *value_ptr = lookup.lookup_ptr(key);
    if (value_ptr == nullptr) {
      return std::nullopt;
    }
    const DictionaryValue::LookupValue &value = *value_ptr;

    if (value->type() != eValueType::String) {
      return std::nullopt;
    }

    return value->as_string_value()->value();
  }

  const ArrayValue *get_array(StringRef key) const
  {
    const DictionaryValue::LookupValue *value_ptr = lookup.lookup_ptr(key);
    if (value_ptr == nullptr) {
      return nullptr;
    }
    const DictionaryValue::LookupValue &value = *value_ptr;

    if (value->type() != eValueType::Array) {
      return nullptr;
    }

    return value->as_array_value();
  }

  std::optional<int32_t> get_int(StringRef key) const
  {
    const DictionaryValue::LookupValue *value_ptr = lookup.lookup_ptr(key);
    if (value_ptr == nullptr) {
      return std::nullopt;
    }
    const DictionaryValue::LookupValue &value = *value_ptr;

    if (value->type() != eValueType::Int) {
      return std::nullopt;
    }

    return value->as_int_value()->value();
  }

  std::optional<double> get_double(StringRef key) const
  {
    const DictionaryValue::LookupValue *value_ptr = lookup.lookup_ptr(key);
    if (value_ptr == nullptr) {
      return std::nullopt;
    }
    const DictionaryValue::LookupValue &value = *value_ptr;

    if (value->type() != eValueType::Double) {
      return std::nullopt;
    }

    return value->as_double_value()->value();
  }

  std::optional<float> get_float(StringRef key) const
  {
    return static_cast<std::optional<float>>(get_double(key));
  }

  template<typename PrimitiveType, typename ValueType>
  std::optional<Vector<PrimitiveType>> get_array_primitive(StringRef key) const
  {
    const DictionaryValue::LookupValue *value_ptr = lookup.lookup_ptr(key);
    if (value_ptr == nullptr) {
      return std::nullopt;
    }
    const DictionaryValue::LookupValue &value = *value_ptr;

    if (value->type() != eValueType::Array) {
      return std::nullopt;
    }

    Vector<PrimitiveType> result;
    const ArrayValue::Items &elements = value->as_array_value()->elements();
    for (const ArrayValue::Item &element : elements) {
      const ValueType *value_type = static_cast<const ValueType *>(element.get());
      PrimitiveType primitive_value = value_type->value();
      result.append_as(primitive_value);
    }

    return result;
  }

  std::optional<eIDPropertyType> get_id_property_type(StringRef key) const
  {
    std::optional<std::string> string_value = get_string(key);
    if (!string_value.has_value()) {
      return std::nullopt;
    }
    const IDPropertySerializer &serializer = serializer_for(*string_value);
    return serializer.property_type();
  }
};

/** \brief IDPSerializer for IDP_STRING. */
class IDPStringSerializer : public IDPropertySerializer {
 public:
  constexpr IDPStringSerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_STRING;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_STRING;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    attributes.append_as(std::pair(IDP_KEY_VALUE, new StringValue(IDP_String(id_property))));
    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_STRING);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<std::string> string_value = entry_reader.get_string_value();
    if (!string_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), string_value->c_str());
  }
};

/** \brief IDPSerializer for IDP_INT. */
class IDPIntSerializer : public IDPropertySerializer {
 public:
  constexpr IDPIntSerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_INT;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_INT;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    attributes.append_as(std::pair(IDP_KEY_VALUE, new IntValue(IDP_Int(id_property))));
    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_INT);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<int32_t> extracted_value = entry_reader.get_int_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }
};

/** \brief IDPSerializer for IDP_FLOAT. */
class IDPFloatSerializer : public IDPropertySerializer {
 public:
  constexpr IDPFloatSerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_FLOAT;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_FLOAT;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    attributes.append_as(std::pair(IDP_KEY_VALUE, new DoubleValue(IDP_Float(id_property))));
    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_FLOAT);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<float> extracted_value = entry_reader.get_float_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }
};

/** \brief IDPSerializer for IDP_DOUBLE. */
class IDPDoubleSerializer : public IDPropertySerializer {
 public:
  constexpr IDPDoubleSerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_DOUBLE;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_DOUBLE;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    attributes.append_as(std::pair(IDP_KEY_VALUE, new DoubleValue(IDP_Double(id_property))));
    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_DOUBLE);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<double> extracted_value = entry_reader.get_double_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }
};

/** \brief IDPSerializer for IDP_ARRAY. */
class IDPArraySerializer : public IDPropertySerializer {
 public:
  constexpr IDPArraySerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_ARRAY;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_ARRAY;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    const IDPropertySerializer &subtype_serializer = serializer_for(
        static_cast<eIDPropertyType>(id_property->subtype));
    attributes.append_as(
        std::pair(IDP_KEY_SUBTYPE, new StringValue(subtype_serializer.type_name())));

    std::shared_ptr<ArrayValue> array = std::make_shared<ArrayValue>();
    switch (static_cast<eIDPropertyType>(id_property->subtype)) {
      case IDP_INT: {
        int32_t *values = static_cast<int32_t *>(IDP_Array(id_property));
        add_values<int32_t, IntValue>(array.get(), Span<int32_t>(values, id_property->len));
        break;
      }

      case IDP_FLOAT: {
        float *values = static_cast<float *>(IDP_Array(id_property));
        add_values<float, DoubleValue>(array.get(), Span<float>(values, id_property->len));
        break;
      }

      case IDP_DOUBLE: {
        double *values = static_cast<double *>(IDP_Array(id_property));
        add_values<double, DoubleValue>(array.get(), Span<double>(values, id_property->len));
        break;
      }

      case IDP_GROUP: {
        IDProperty *values = static_cast<IDProperty *>(IDP_Array(id_property));
        add_values(array.get(), Span<IDProperty>(values, id_property->len));
        break;
      }

      default: {
        /* IDP_ARRAY only supports IDP_INT, IDP_FLOAT, IDP_DOUBLE and IDP_GROUP. */
        BLI_assert_unreachable();
        break;
      }
    }
    attributes.append_as(std::pair(IDP_KEY_VALUE, std::move(array)));

    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
    std::optional<eIDPropertyType> property_subtype = entry_reader.get_subtype();
    if (!property_subtype.has_value()) {
      return nullptr;
    }

    switch (*property_subtype) {
      case IDP_INT:
        return idprop_array_int_from_value(entry_reader);

      case IDP_FLOAT:
        return idprop_array_float_from_value(entry_reader);

      case IDP_DOUBLE:
        return idprop_array_double_from_value(entry_reader);

      default:
        break;
    }
    return nullptr;
  }

 private:
  /** Add the given values to array. */
  template</* C-primitive type of the values to add. Possible types are `float`, `int32_t` or
            * `double`. */
           typename PrimitiveType,
           /* Type of value that can store the PrimitiveType in the Array. */
           typename ValueType>
  void add_values(ArrayValue *array, Span<PrimitiveType> values) const
  {
    ArrayValue::Items &items = array->elements();
    for (PrimitiveType value : values) {
      items.append_as(std::make_shared<ValueType>(value));
    }
  }

  void add_values(ArrayValue *array, Span<IDProperty> values) const
  {
    ArrayValue::Items &items = array->elements();
    for (const IDProperty &id_property : values) {
      const IDPropertySerializer &value_serializer = serializer_for(
          static_cast<eIDPropertyType>(id_property.type));
      if (!value_serializer.supports_serializing()) {
        continue;
      }
      std::shared_ptr<DictionaryValue> value = value_serializer.idprop_to_dictionary(&id_property);
      items.append_as(value);
    }
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_int_from_value(
      DictionaryEntryParser &entry_reader) const
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
    BLI_assert(*(entry_reader.get_subtype()) == IDP_INT);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<Vector<int32_t>> extracted_value = entry_reader.get_array_int_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_float_from_value(
      DictionaryEntryParser &entry_reader) const
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
    BLI_assert(*(entry_reader.get_subtype()) == IDP_FLOAT);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<Vector<float>> extracted_value = entry_reader.get_array_float_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_double_from_value(
      DictionaryEntryParser &entry_reader) const
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
    BLI_assert(*(entry_reader.get_subtype()) == IDP_DOUBLE);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }
    std::optional<Vector<double>> extracted_value = entry_reader.get_array_double_value();
    if (!extracted_value.has_value()) {
      return nullptr;
    }
    return create(name->c_str(), *extracted_value);
  }
};

/** \brief IDPSerializer for IDP_GROUP. */
class IDPGroupSerializer : public IDPropertySerializer {
 public:
  constexpr IDPGroupSerializer() = default;

  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_GROUP;
  }

  std::optional<eIDPropertyType> property_type() const override
  {
    return IDP_GROUP;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty *id_property) const override
  {
    std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
    DictionaryValue::Items &attributes = result->elements();
    std::shared_ptr<ArrayValue> array = std::make_shared<ArrayValue>();
    ArrayValue::Items &elements = array->elements();

    LISTBASE_FOREACH (IDProperty *, sub_property, &id_property->data.group) {

      const IDPropertySerializer &sub_property_serializer = serializer_for(
          static_cast<eIDPropertyType>(sub_property->type));
      elements.append_as(sub_property_serializer.idprop_to_dictionary(sub_property));
    }

    attributes.append_as(std::pair(IDP_KEY_VALUE, array));
    return result;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser &entry_reader) const override
  {
    BLI_assert(*(entry_reader.get_type()) == IDP_GROUP);
    std::optional<std::string> name = entry_reader.get_name();
    if (!name.has_value()) {
      return nullptr;
    }

    const ArrayValue *array = entry_reader.get_array_value();
    if (array == nullptr) {
      return nullptr;
    }

    std::unique_ptr<IDProperty, IDPropertyDeleter> result = create_group(name->c_str());
    for (const ArrayValue::Item &element : array->elements()) {
      if (element->type() != eValueType::Dictionary) {
        continue;
      }
      const DictionaryValue *subobject = element->as_dictionary_value();
      IDProperty *subproperty = idprop_from_value(*subobject);
      IDP_AddToGroup(result.get(), subproperty);
    }

    return result;
  }
};

/**
 * \brief Dummy serializer for unknown and unsupported types.
 */
class IDPUnknownSerializer : public IDPropertySerializer {
 public:
  constexpr IDPUnknownSerializer() = default;
  std::string type_name() const override
  {
    return IDP_PROPERTY_TYPENAME_UNKNOWN;
  }
  std::optional<eIDPropertyType> property_type() const override
  {
    return std::nullopt;
  }

  std::shared_ptr<DictionaryValue> idprop_to_dictionary(
      const struct IDProperty * /*id_property*/) const override
  {
    BLI_assert_unreachable();
    return nullptr;
  }

  bool supports_serializing() const override
  {
    return false;
  }

  std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
      DictionaryEntryParser & /*entry_reader*/) const override
  {
    return nullptr;
  }
};

/* Serializers are constructed statically to remove construction/destruction. */
static constexpr IDPStringSerializer IDP_SERIALIZER_STRING;
static constexpr IDPIntSerializer IDP_SERIALIZER_INT;
static constexpr IDPFloatSerializer IDP_SERIALIZER_FLOAT;
static constexpr IDPDoubleSerializer IDP_SERIALIZER_DOUBLE;
static constexpr IDPArraySerializer IDP_SERIALIZER_ARRAY;
static constexpr IDPGroupSerializer IDP_SERIALIZER_GROUP;
static constexpr IDPUnknownSerializer IDP_SERIALIZER_UNKNOWN;

/** \brief get the serializer for the given property type. */
static const IDPropertySerializer &serializer_for(eIDPropertyType property_type)
{
  switch (property_type) {
    case IDP_STRING:
      return IDP_SERIALIZER_STRING;

    case IDP_INT:
      return IDP_SERIALIZER_INT;

    case IDP_FLOAT:
      return IDP_SERIALIZER_FLOAT;

    case IDP_DOUBLE:
      return IDP_SERIALIZER_DOUBLE;

    case IDP_ARRAY:
      return IDP_SERIALIZER_ARRAY;

    case IDP_GROUP:
      return IDP_SERIALIZER_GROUP;

    default:
      BLI_assert_msg(false, "Trying to convert an unsupported/unknown property type to a string");
      return IDP_SERIALIZER_UNKNOWN;
  }
}

/** \brief get serializer for the given typename. */
static const IDPropertySerializer &serializer_for(StringRef idprop_typename)
{
  if (idprop_typename == IDP_PROPERTY_TYPENAME_STRING) {
    return IDP_SERIALIZER_STRING;
  }
  if (idprop_typename == IDP_PROPERTY_TYPENAME_INT) {
    return IDP_SERIALIZER_INT;
  }
  if (idprop_typename == IDP_PROPERTY_TYPENAME_FLOAT) {
    return IDP_SERIALIZER_FLOAT;
  }
  if (idprop_typename == IDP_PROPERTY_TYPENAME_DOUBLE) {
    return IDP_SERIALIZER_DOUBLE;
  }
  if (idprop_typename == IDP_PROPERTY_TYPENAME_ARRAY) {
    return IDP_SERIALIZER_ARRAY;
  }
  if (idprop_typename == IDP_PROPERTY_TYPENAME_GROUP) {
    return IDP_SERIALIZER_GROUP;
  }
  return IDP_SERIALIZER_UNKNOWN;
}

/* \} */

/* -------------------------------------------------------------------- */
/** \name IDProperty to Value
 * \{ */
std::unique_ptr<ArrayValue> convert_to_serialize_values(const struct IDProperty *properties)
{
  BLI_assert(properties != nullptr);
  std::unique_ptr<ArrayValue> result = std::make_unique<ArrayValue>();
  ArrayValue::Items &elements = result->elements();
  const struct IDProperty *current_property = properties;
  while (current_property != nullptr) {
    const IDPropertySerializer &serializer = serializer_for(
        static_cast<eIDPropertyType>(current_property->type));
    if (serializer.supports_serializing()) {
      elements.append_as(serializer.idprop_to_dictionary(current_property));
    }
    current_property = current_property->next;
  }

  return result;
}

/* \} */

/* -------------------------------------------------------------------- */
/** \name IDProperty from Value
 * \{ */

static IDProperty *idprop_from_value(const DictionaryValue &value)
{
  DictionaryEntryParser entry_reader(value);
  std::optional<eIDPropertyType> property_type = entry_reader.get_type();
  if (!property_type.has_value()) {
    return nullptr;
  }

  const IDPropertySerializer &serializer = serializer_for(*property_type);
  return serializer.entry_to_idprop(entry_reader).release();
}

static IDProperty *idprop_from_value(const ArrayValue &value)
{
  IDProperty *result = nullptr;
  IDProperty *previous_added = nullptr;

  const ArrayValue::Items &elements = value.elements();
  for (const ArrayValue::Item &element : elements) {
    if (element->type() != eValueType::Dictionary) {
      continue;
    }
    const DictionaryValue *object_value = element->as_dictionary_value();
    IDProperty *last_created = idprop_from_value(*object_value);
    if (last_created == nullptr) {
      continue;
    }

    if (result == nullptr) {
      result = last_created;
    }

    if (previous_added) {
      previous_added->next = last_created;
    }
    last_created->prev = previous_added;
    previous_added = last_created;
  }

  return result;
}

IDProperty *convert_from_serialize_value(const Value &value)
{
  if (value.type() != eValueType::Array) {
    return nullptr;
  }

  return idprop_from_value(*value.as_array_value());
}

/* \} */

}  // namespace blender::bke::idprop