JSONformatter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "JSONformatter.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(JSONformatter, 0);
35 }
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 bool Foam::JSONformatter::writeToken(const token& t)
40 {
41  bool ok = true;
42  switch (t.type())
43  {
44  case token::tokenType::BOOL:
45  write(t.boolToken());
46  break;
47 
48  case token::tokenType::LABEL:
49  write(t.labelToken());
50  break;
51 
52  case token::tokenType::FLOAT:
53  case token::tokenType::DOUBLE:
54  write(t.scalarToken());
55  break;
56 
57  case token::tokenType::WORD:
58  case token::tokenType::DIRECTIVE:
59  write(t.wordToken());
60  break;
61 
62  case token::tokenType::STRING:
63  case token::tokenType::EXPRESSION:
64  case token::tokenType::VARIABLE:
65  case token::tokenType::VERBATIM:
66  case token::tokenType::CHAR_DATA:
67  write(t.stringToken());
68  break;
69 
70  default:
71  DebugInfo
72  << "Problem converting token to JSON:" << nl
73  << " " << Foam::name(int(t.type()))
74  << " - treating as null" << endl;
75 
76  os_ << "null";
77 
78  ok = false;
79  break;
80  }
81 
82  return ok;
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
89 :
90  os_(os)
91 {}
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
97 (
98  const keyType& keyword
99 )
100 {
101  return os_.writeQuoted(keyword);
102 }
103 
104 
106 {
107  if (dict.empty())
108  {
109  os_ << "{}";
110  return os_;
111  }
112 
113  os_ << '{' << nl << incrIndent;
114 
115  const auto openBrace = [](const token& t){
116  return t == token::BEGIN_LIST || t == token::BEGIN_SQR;
117  };
118  const auto closeBrace = [](const token& t){
119  return t == token::END_LIST || t == token::END_SQR;
120  };
121 
122  label entryi = 0;
123  for (const entry& e : dict)
124  {
125  if (entryi)
126  {
127  os_ << ',' << nl;
128  }
129 
130  const word& keyword = e.keyword();
131 
132  os_ << indent;
133  os_.writeQuoted(keyword) << " : ";
134 
135  if (e.isDict())
136  {
137  writeDict(e.dict());
138 
139  ++entryi;
140 
141  continue;
142  }
143 
144  const auto& tokens = e.stream();
145 
146  if (tokens.empty()) continue; // error?
147 
148  if (tokens.size() == 1)
149  {
150  writeToken(tokens[0]);
151  }
152  else
153  {
154  label offset = 0;
155  if (tokens[0].isLabel() && openBrace(tokens[1]))
156  {
157  offset = 1; // reading 'size (value0 value1 ... valueN)
158  }
159 
160  const token& t = tokens[offset];
161  if (openBrace(t))
162  {
163  // Assume array-type
164  label i = 0;
165  for (label tokeni=offset; tokeni<tokens.size(); ++tokeni)
166  {
167  const token& tk = tokens[tokeni];
168 
169  if (openBrace(tk))
170  {
171  if (i) os_ << ',';
172  os_ << '[';
173  i = 0;
174  }
175  else if (closeBrace(tk))
176  {
177  os_ << ']';
178  }
179  else
180  {
181  if (i) os_ << ',';
182  if (writeToken(tk)) ++i;
183 
184  if (i % 10 == 0) os_ << nl;
185  }
186  }
187  }
188  else
189  {
190  // Unknown type - convert to string representation
191  os_ << '"';
192  forAll(tokens, tokeni)
193  {
194  if (tokeni) os_ << ' ';
195  os_ << tokens[tokeni];
196  }
197  os_ << '"';
198  }
199  }
200 
201  ++entryi;
202  }
204  os_ << nl << decrIndent << indent << '}';
205 
206  return os_;
207 }
208 
209 
211 {
212  os_ << (val ? "true" : "false");
213  return os_;
214 }
215 
216 
218 {
219  os_ << val;
220  return os_;
221 }
222 
223 
225 {
226  os_ << val;
227  return os_;
228 }
229 
230 
232 {
233  os_ << val;
234  return os_;
235 }
236 
237 
239 {
240  os_ << val;
241  return os_;
242 }
243 
246 {
247  return os_.writeQuoted(str);
248 }
249 
251 Foam::Ostream& Foam::JSONformatter::write(const std::string& str)
252 {
253  return os_.writeQuoted(str);
254 }
255 
256 
258 {
259  os_ << c;
260  return os_;
261 }
262 
263 
264 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
dictionary dict
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
virtual Ostream & writeKeyword(const keyType &keyword)
Write JSON keyword.
Definition: JSONformatter.C:90
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Begin dimensions [isseparator].
Definition: token.H:163
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Begin list [isseparator].
Definition: token.H:161
virtual Ostream & writeDict(const dictionary &dict)
Write OpenFOAM dictionary to JSON dictionary.
Definition: JSONformatter.C:98
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
End dimensions [isseparator].
Definition: token.H:164
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
JSONformatter(Ostream &os)
Construct from Ostream.
Definition: JSONformatter.C:81
End list [isseparator].
Definition: token.H:162
#define DebugInfo
Report an information message using Foam::Info.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
const dimensionedScalar c
Speed of light in a vacuum.
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:502
Namespace for OpenFOAM.
virtual Ostream & write(const bool val)