Solution46
CJsonPair.cpp
[詳解]
1 //=============================================================================
2 /// @file
3 /// JSONペアクラス実装ファイル
4 ///
5 /// JSONペアクラス実装ファイルです。
6 ///
7 /// $Id: CJsonPair.cpp 245 2019-03-20 15:03:41Z admin $
8 /// $Date: 2019-03-21 00:03:41 +0900 (2019/03/21 (木)) $
9 /// $Author: admin $
10 ///
11 /// @attention なし
12 
13 #pragma managed( push, off )
14 
15 //=============================================================================
16 // インクルードファイル
17 #include <CJsonPair.h>
18 #include <CJsonFixed.h>
19 
20 //=============================================================================
21 // JSONライブラリ名前空間
22 namespace LibJson {
23  //=========================================================================
24  // JSONペアクラス
25  //=========================================================================
26  // 構築子と解体子
27  //-------------------------------------------------------------------------
28  // コンストラクタ
29  CJsonPair::CJsonPair( CJson* pcParent, wchar_t const* pszString ) noexcept
30  // 基底クラスコンストラクタ
31  : CJson( pcParent )
32  // メンバ変数初期化
33  , m_pcJsonString( new CJsonString( this, pszString ) )
34  , m_pcJsonValue( new CJsonFixed( this, L"null" ) )
35  {}
36 
37  //-------------------------------------------------------------------------
38  // デストラクタ
39  CJsonPair::~CJsonPair() noexcept {
40  // クリアする
41  ClearJson();
42  }
43 
44  //=========================================================================
45  // 公開関数
46  //-------------------------------------------------------------------------
47  // JSONクラスクリア関数
48  void CJsonPair::ClearJson() noexcept {
49  // 基底クラスをクリアする
51 
52  // JSONバリューを削除する
53  delete m_pcJsonValue;
54  m_pcJsonValue = nullptr;
55 
56  // JSON文字列を削除する
57  delete m_pcJsonString;
58  m_pcJsonString = nullptr;
59  }
60 
61  //-------------------------------------------------------------------------
62  // JSONクラスコンパクト化関数
63  bool CJsonPair::CompactJson() noexcept {
64  // 処理ブロック
65  bool result = false;
66  do {
67  // 基底クラスをコンパクト化する
68  if ( !CJson::CompactJson() ) {
69  // 失敗!
70  break;
71  }
72  // JSON文字列ポインタを調べる
73  else if ( nullptr != m_pcJsonString ) {
74  // JSON文字列をコンパクト化する
75  if ( !m_pcJsonString->CompactJson() ) {
76  // 失敗!
77  break;
78  }
79  }
80 
81  // JSONバリューポインタを調べる
82  if ( nullptr != m_pcJsonValue ) {
83  // JSONバリューをコンパクト化する
84  if ( !m_pcJsonValue->CompactJson() ) {
85  // 失敗!
86  break;
87  }
88  }
89 
90  // 成功!
91  result = true;
92  } while ( false );
93 
94  // 実行結果を返す
95  return result;
96  }
97 
98  //-------------------------------------------------------------------------
99  // JSONクラス入力関数
100  bool CJsonPair::InputJson( CStream& rcStream ) noexcept {
101  // 処理ブロック
102  bool result = false;
103  do {
104  // クリアする
105  ClearJson();
106 
107  // ストリームポインタをプッシュする
108  if ( !rcStream.PushStreamPoint( m_pcInputPointerList ) ) {
109  // 失敗!
110  break;
111  }
112  // 処理ブロック
113  else do {
114  // JSON文字列を入力作成する
115  m_pcJsonString = CJsonString::CreateInputJson( rcStream, this );
116  if ( nullptr == m_pcJsonString ) {
117  // 失敗!
118  break;
119  }
120 
121  // 空白をスキップする
122  rcStream.SkipSpace();
123 
124  // 1文字スキップする
125  if ( !rcStream.SkipChar( L':' ) ) {
126  // クリアする
127  ClearJson();
128 
129  // 失敗!
130  break;
131  }
132 
133  // JSONバリューを入力作成する
134  m_pcJsonValue = CJsonValue::CreateInputJson( rcStream, this );
135  if ( nullptr == m_pcJsonValue ) {
136  // クリアする
137  ClearJson();
138 
139  // 失敗!
140  break;
141  }
142 
143  // 成功!
144  result = true;
145  } while ( false );
146 
147  // 入力ストリームポインタをポップする
148  rcStream.PopStreamPoint( m_pcInputPointerList, !result );
149  } while ( false );
150 
151  // 実行結果を返す
152  return result;
153  }
154 
155  //-------------------------------------------------------------------------
156  // JSONクラス出力関数
157  bool CJsonPair::OutputJson( CStreamOut& rcStreamOut ) noexcept {
158  // 処理ブロック
159  bool result = false;
160  do {
161  // JSON文字列ポインタを調べる
162  if ( nullptr == m_pcJsonString ) {
163  // 失敗!
164  break;
165  }
166  // JSON文字列を出力する
167  else if ( !m_pcJsonString->OutputJson( rcStreamOut ) ) {
168  // 失敗!
169  break;
170  }
171  // 区切り文字列を出力する
172  else if ( !rcStreamOut.OutputString( L": " ) ) {
173  // 失敗!
174  break;
175  }
176  // JSONバリューポインタを調べる
177  else if ( nullptr == m_pcJsonValue ) {
178  // 失敗!
179  break;
180  }
181  // JSONバリューを出力する
182  else if ( !m_pcJsonValue->OutputJson( rcStreamOut ) ) {
183  // 失敗!
184  break;
185  }
186 
187  // 成功!
188  result = true;
189  } while ( false );
190 
191  // 実行結果を返す
192  return result;
193  }
194 
195  //=========================================================================
196  // 静的公開関数
197  //-------------------------------------------------------------------------
198  // JSONペア入力作成関数
199  CJsonPair* CJsonPair::CreateInputJson( CStream& rcStream, CJson* pcParent ) noexcept {
200  // 処理ブロック
201  CJsonPair* result = nullptr;
202  do {
203  // JSONペアを作成する
204  CJsonPair* pcJsonPair = new CJsonPair( pcParent );
205  if ( nullptr == pcJsonPair ) {
206  // 失敗!
207  break;
208  }
209  // JSONペアを入力する
210  else if ( !pcJsonPair->InputJson( rcStream ) ) {
211  // JSONペアを削除する
212  delete pcJsonPair;
213 
214  // 失敗!
215  break;
216  }
217 
218  // 成功!
219  result = pcJsonPair;
220  } while ( false );
221 
222  // 実行結果を返す
223  return result;
224  }
225 
226  //-------------------------------------------------------------------------
227  // JSONバリュー設定関数
228  bool CJsonPair::SetJsonValue( CJsonValue* pcJsonValue ) noexcept {
229  // 処理ブロック
230  bool result = false;
231  do {
232  // JSONバリューを削除する
233  delete m_pcJsonValue;
234 
235  // JSONバリューポインタを設定する
236  m_pcJsonValue = pcJsonValue;
237 
238  // 成功!
239  result = true;
240  } while ( false );
241 
242  // 実行結果を返す
243  return result;
244  }
245 }
246 
247 #pragma managed( pop )
JSONペアクラス
Definition: CJsonPair.h:32
virtual bool OutputJson(CStreamOut &rcStreamOut) noexcept
JSONクラス出力関数
Definition: CJson.h:232
CJsonPair(CJson *pcParent, wchar_t const *pszString=nullptr) noexcept
コンストラクタ
Definition: CJsonPair.cpp:29
JSONライブラリ名前空間
Definition: CJson.h:24
CJsonString * m_pcJsonString
JSON文字列ポインタ
Definition: CJsonPair.h:183
JSONバリュークラス
Definition: CJsonValue.h:30
virtual bool CompactJson() noexcept override
JSONクラスコンパクト化関数
Definition: CJsonPair.cpp:63
ストリームクラス
Definition: CStream.h:31
JSONペアクラスヘッダファイル
JSONクラス
Definition: CJson.h:44
ストリーム出力クラス
Definition: CStreamOut.h:30
CArray< int > * m_pcInputPointerList
入力ストリームポインタリストポインタ
Definition: CJson.h:238
static CJsonString * CreateInputJson(CStream &rcStream, CJson *pcParent) noexcept
JSON文字列入力作成関数
virtual bool SetJsonValue(CJsonValue *pcJsonValue) noexcept
JSONバリュー設定関数
Definition: CJsonPair.cpp:228
JSON固定値クラス
Definition: CJsonFixed.h:30
virtual bool CompactJson() noexcept
JSONクラスコンパクト化関数
Definition: CJson.cpp:93
JSON固定値クラスヘッダファイル
virtual bool OutputJson(CStreamOut &rcStreamOut) noexcept override
JSONクラス出力関数
Definition: CJsonString.cpp:89
virtual void ClearJson() noexcept override
JSONクラスクリア関数
Definition: CJsonPair.cpp:48
virtual ~CJsonPair() noexcept
デストラクタ
Definition: CJsonPair.cpp:39
static CJsonValue * CreateInputJson(CStream &rcStream, CJson *pcParent) noexcept
JSONバリュー入力作成関数
Definition: CJsonValue.cpp:46
virtual bool InputJson(CStream &rcStream) noexcept override
JSONクラス入力関数
Definition: CJsonPair.cpp:100
virtual bool CompactJson() noexcept override
JSONクラスコンパクト化関数
Definition: CJsonToken.cpp:72
JSON文字列クラス
Definition: CJsonString.h:30
static CJsonPair * CreateInputJson(CStream &rcStream, CJson *pcParent) noexcept
JSONペア入力作成関数
Definition: CJsonPair.cpp:199
virtual bool OutputJson(CStreamOut &rcStreamOut) noexcept override
JSONクラス出力関数
Definition: CJsonPair.cpp:157
virtual void ClearJson() noexcept
JSONクラスクリア関数
Definition: CJson.cpp:86
CJsonValue * m_pcJsonValue
JSONバリューポインタ
Definition: CJsonPair.h:184