JSONformatter.H
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 Class
27  Foam::JSONformatter
28 
29 Description
30  An wrapper for Ostream that outputs content in JSON format.
31 
32 SourceFiles
33  JSONformatter.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_JSONformatter_H
38 #define Foam_JSONformatter_H
39 
40 #include "Ostream.H"
41 #include "dictionary.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class JSONformatter Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 class JSONformatter
53 {
54  // Private Data
55 
56  //- Reference to the output stream
57  Ostream& os_;
58 
59 
60  // Private Member Functions
61 
62  //- Write token to stream
63  bool writeToken(const token& t);
64 
65 
66 public:
67 
68  //- Declare type-name
69  TypeName("JSONformatter");
70 
71  //- Construct from Ostream
72  explicit JSONformatter(Ostream& os);
73 
74  //- Destructor
75  virtual ~JSONformatter() = default;
76 
77 
78  // Member Functions
79 
80  // Write Functions
81 
82  virtual Ostream& write(const bool val);
83  virtual Ostream& write(const int32_t val);
84  virtual Ostream& write(const int64_t val);
85  virtual Ostream& write(const float val);
86  virtual Ostream& write(const double val);
87  virtual Ostream& write(const word& str);
88  virtual Ostream& write(const std::string& str);
89  virtual Ostream& write(const char* str);
90 
91  //- Write JSON keyword
92  virtual Ostream& writeKeyword(const keyType& keyword);
93 
94  //- Write OpenFOAM dictionary to JSON dictionary
95  virtual Ostream& writeDict(const dictionary& dict);
96 
97  //- Write JSON keyword-value pair (for primitive types)
98  template<class Type>
99  Ostream& writeEntry(const word& keyword, const Type& val)
100  {
101  os_.writeQuoted(keyword) << " : ";
102  write(val);
103  return os_;
104  }
105 };
106 
107 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
108 
109 } // End namespace Foam
111 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
112 
113 #endif
114 
115 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
dictionary dict
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
A token holds an item read from Istream.
Definition: token.H:65
TypeName("JSONformatter")
Declare type-name.
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.
A class for handling words, derived from Foam::string.
Definition: word.H:63
An wrapper for Ostream that outputs content in JSON format.
Definition: JSONformatter.H:47
JSONformatter(Ostream &os)
Construct from Ostream.
Definition: JSONformatter.C:81
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
Ostream & writeEntry(const word &keyword, const Type &val)
Write JSON keyword-value pair (for primitive types)
virtual ~JSONformatter()=default
Destructor.
Namespace for OpenFOAM.
virtual Ostream & write(const bool val)