Solution46
CJsonObject.cpp
[詳解]
1 //=============================================================================
2 /// @file
3 /// JSONオブジェクトクラス実装ファイル
4 ///
5 /// JSONオブジェクトクラス実装ファイルです。
6 ///
7 /// $Id: CJsonObject.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 <CJsonObject.h>
18 
19 //=============================================================================
20 // インクルード実装ファイル
21 #include <CArray.hpp>
22 
23 //=============================================================================
24 // JSONライブラリ名前空間
25 namespace LibJson {
26  //=========================================================================
27  // JSONオブジェクトクラス
28  //=========================================================================
29  // 構築子と解体子
30  //-------------------------------------------------------------------------
31  // コンストラクタ
32  CJsonObject::CJsonObject( CJson* pcParent ) noexcept
33  // 基底クラスコンストラクタ
34  : CJsonContainer( pcParent )
35  {}
36 
37  //=========================================================================
38  // 公開関数
39  //-------------------------------------------------------------------------
40  // JSONペア取得関数
41  CJsonPair* CJsonObject::GetJsonPair( wchar_t const* pszName ) noexcept {
42  // 処理ブロック
43  CJsonPair* result = nullptr;
44  do {
45  // JSONエレメントインデックスを取得する
46  int nIndex = GetJsonElementIndex( pszName );
47  if ( 0 > nIndex ) {
48  // 失敗!
49  break;
50  }
51 
52  // JSONペアを取得する
53  CJsonPair* pcJsonPair = ( *this )[ nIndex ]->GetJsonPair();
54  if ( nullptr == pcJsonPair ) {
55  // 失敗!
56  break;
57  }
58 
59  // 成功!
60  result = pcJsonPair;
61  } while ( false );
62 
63  // 実行結果を返す
64  return result;
65  }
66 
67  //-------------------------------------------------------------------------
68  // JSONエレメントインデックス取得関数
69  int CJsonObject::GetJsonElementIndex( wchar_t const* pszName ) noexcept {
70  // 処理ブロック
71  int result = -1;
72  do {
73  // 名前文字列ポインタを調べる
74  if ( nullptr == pszName ) {
75  // 失敗!
76  break;
77  }
78  // 巡回する
79  else for ( int nIndex = 0; this->GetCount() > nIndex; ++nIndex ) {
80  // JSONクラスを取得する
81  CJson* pcJson = ( *this )[ nIndex ];
82  if ( nullptr != pcJson ) {
83  // JSONペアを取得する
84  CJsonPair* pcJsonPair = pcJson->GetJsonPair();
85  if ( nullptr != pcJsonPair ) {
86  // JSON文字列を取得する
87  CJsonString* pcJsonString = pcJsonPair->GetJsonString();
88  if ( nullptr != pcJsonString ) {
89  // 名前を取得する
90  wchar_t const* pszString = *pcJsonString;
91  if ( nullptr != pszString ) {
92  // 名前を調べる
93  if ( 0 == ::wcscmp( pszName, pszString ) ) {
94  // 成功!
95  result = nIndex;
96  break;
97  }
98  }
99  }
100  }
101  }
102  }
103  } while ( false );
104 
105  // 実行結果を返す
106  return result;
107  }
108 
109  //=========================================================================
110  // 静的公開関数
111  //-------------------------------------------------------------------------
112  // JSONオブジェクト入力作成関数
113  CJsonObject* CJsonObject::CreateInputJson( CStream& rcStream, CJson* pcParent ) noexcept {
114  // 処理ブロック
115  CJsonObject* result = nullptr;
116  do {
117  // JSONオブジェクトを作成する
118  CJsonObject* pcJsonObject = new CJsonObject( pcParent );
119  if ( nullptr == pcJsonObject ) {
120  // 失敗!
121  break;
122  }
123  // JSONオブジェクトを入力する
124  else if ( !pcJsonObject->InputJson( rcStream ) ) {
125  // JSONオブジェクトを削除する
126  delete pcJsonObject;
127 
128  // 失敗!
129  break;
130  }
131 
132  // 成功!
133  result = pcJsonObject;
134  } while ( false );
135 
136  // 実行結果を返す
137  return result;
138  }
139 }
140 
141 #pragma managed( pop )
JSONペアクラス
Definition: CJsonPair.h:32
JSONオブジェクトクラス
Definition: CJsonObject.h:31
virtual CJsonString * GetJsonString() noexcept override
Definition: CJsonPair.h:96
CJsonObject(CJson *pcParent) noexcept
コンストラクタ
Definition: CJsonObject.cpp:32
virtual CJsonPair * GetJsonPair() noexcept override
JSONクラスインスタンス取得関数
Definition: CJsonPair.h:95
JSONライブラリ名前空間
Definition: CJson.h:24
配列クラス実装ヘッダファイル
static CJsonObject * CreateInputJson(CStream &rcStream, CJson *pcParent) noexcept
JSONオブジェクト入力作成関数
virtual bool InputJson(CStream &rcStream) noexcept override
JSONクラス入力関数
JSONコンテナクラス
ストリームクラス
Definition: CStream.h:31
JSONクラス
Definition: CJson.h:44
virtual int GetJsonElementIndex(wchar_t const *pszName) noexcept
JSONエレメントインデックス取得関数
Definition: CJsonObject.cpp:69
virtual CJsonPair * GetJsonPair() noexcept
Definition: CJson.h:165
JSONオブジェクトクラスヘッダファイル
JSON文字列クラス
Definition: CJsonString.h:30