Arcane  4.2.1.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-2026 */
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#include <iostream>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::properties
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36template <typename T>
37class JSONPropertyReader
38: public PropertyVisitor<T>
39{
40 public:
41
42 JSONPropertyReader(JSONValue jv, T& instance)
43 : m_jv(jv)
44 , m_instance(instance)
45 {}
46
47 private:
48
49 JSONValue m_jv;
50 T& m_instance;
51
52 public:
53
54 void visit(const PropertySettingBase<T>& s) override
55 {
56 JSONValue child_value = m_jv.child(s.setting()->name());
57 if (child_value.null())
58 return;
59 s.setFromJSON(child_value, m_instance);
60 s.print(std::cout, m_instance);
61 }
62};
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
73template <typename T, typename PropertyType = T> inline void
74readFromJSON(JSONValue jv, T& instance)
75{
76 const char* instance_property_name = PropertyType ::propertyClassName();
77 JSONValue child_value = jv.child(instance_property_name);
78 if (child_value.null())
79 return;
80 JSONPropertyReader reader(child_value, instance);
81 PropertyType ::applyPropertyVisitor(reader);
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
87} // End namespace Arcane::properties
88
89/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
91
92#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