Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
JSONPropertyReader.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* JSONPropertyReader.h (C) 2000-2025 */
9/* */
10/* Reading properties in JSON format. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_INTERNAL_JSONPROPERTYREADER_H
13#define ARCANE_UTILS_INTERNAL_JSONPROPERTYREADER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17/*
18 * NOTE: The classes in this file are under development.
19 * NOTE: The API may change at any time. Do not use outside of Arcane.
20 */
21
22#include "arccore/common/JSONReader.h"
23#include "arccore/common/internal/Property.h"
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane::properties
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34template <typename T>
35class JSONPropertyReader
36: public PropertyVisitor<T>
37{
38 public:
39
40 JSONPropertyReader(JSONValue jv, T& instance)
41 : m_jv(jv)
42 , m_instance(instance)
43 {}
44
45 private:
46
47 JSONValue m_jv;
48 T& m_instance;
49
50 public:
51
52 void visit(const PropertySettingBase<T>& s) override
53 {
54 JSONValue child_value = m_jv.child(s.setting()->name());
55 if (child_value.null())
56 return;
57 s.setFromJSON(child_value, m_instance);
58 s.print(std::cout, m_instance);
59 }
60};
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
71template <typename T, typename PropertyType = T> inline void
72readFromJSON(JSONValue jv, T& instance)
73{
74 const char* instance_property_name = PropertyType ::propertyClassName();
75 JSONValue child_value = jv.child(instance_property_name);
76 if (child_value.null())
77 return;
78 JSONPropertyReader reader(child_value, instance);
79 PropertyType ::applyPropertyVisitor(reader);
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85} // End namespace Arcane::properties
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90#endif
bool null() const
True if the node is null.
JSONValue child(StringView name) const
Child value with name name. Returns a null value if not found.
virtual String name() const =0
Property name.
Base class of a property typed by a class.
Definition Property.h:143
Base class of a typed visitor on a property.
Definition Property.h:205