0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder 10.2 Tokyo > JSON > W8111 非推奨のエンティティ '_fastcall TJSONObject::Size()' にアクセスしている (+Get()) > CountとPairs[]を使う (XE4から10.2Tokyoへの移行)

Last updated at Posted at 2017-12-27
動作環境
Rad Studio 10.2 Tokyo Update 2

関連: c++ builder / JSON > ""付きの文字列の扱いに失敗する > "\"ではなく"\\"にする

code

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <DBXJSON.hpp>
#include <memory>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

    // Case 1: OK
  String jsonText = L"{\"name\":\"John Smith\",\"age\":\"33\"}";

	// Case 2: NG
//    String jsonText = L"{\"name\":\"C:\\Windows\",\"age\":\"33\"}";

    std::unique_ptr<TStringList> slread(new TStringList);

    TJSONObject *jsonObj = new TJSONObject();
    String jsonKey, jsonValue;
    TJSONPair *pairObj;

    jsonObj = dynamic_cast<TJSONObject*>(TJSONObject::ParseJSONValue(jsonText));

	for(int pi=0; pi < jsonObj->Size(); pi++) { // pair index
        pairObj = jsonObj->Get(pi);
        jsonKey = pairObj->JsonString->Value();
        jsonValue = pairObj->JsonValue->Value();
        ShowMessage(jsonKey + ":" + jsonValue);
    }
    jsonObj->Free();
}

ビルド時に以下の警告が出る。
XE4では出なかった。

[bcc32 警告] Unit1.cpp(38): W8111 非推奨のエンティティ '_fastcall TJSONObject::Size()' にアクセスしている
詳細な解析情報
Unit1.cpp(22): 構文解析対象: void _fastcall TForm1::Button1Click(TObject *)
[bcc32 警告] Unit1.cpp(39): W8111 非推奨のエンティティ '_fastcall TJSONObject::Get(const int)' にアクセスしている
詳細な解析情報
Unit1.cpp(22): 構文解析対象: void _fastcall TForm1::Button1Click(TObject *)

対処

XE4から10.2 Tokyoに移行する場合、下記の対処が必要になりそうだ。

Warning: Size is deprecated. Please use Count.

function Get(const Index: Integer): TJSONPair; overload; deprecated 'Use Pairs property';

jsonObj->Size()jsonObj->Countに変更すればいい。
jsonObj->Get(pi)jsonObj->Pairs[pi]に変更すればいい。

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <DBXJSON.hpp>
#include <memory>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

    // Case 1: OK
  String jsonText = L"{\"name\":\"John Smith\",\"age\":\"33\"}";

	// Case 2: NG
//    String jsonText = L"{\"name\":\"C:\\Windows\",\"age\":\"33\"}";

	std::unique_ptr<TStringList> slread(new TStringList);

	TJSONObject *jsonObj = new TJSONObject();
	String jsonKey, jsonValue;
	TJSONPair *pairObj;

	jsonObj = dynamic_cast<TJSONObject*>(TJSONObject::ParseJSONValue(jsonText));

	//for(int pi=0; pi < jsonObj->Size(); pi++) { // pair index
	for(int pi=0; pi < jsonObj->Count; pi++) { // pair index // *** 2017-12-27 ***
		//pairObj = jsonObj->Get(pi);
		pairObj = jsonObj->Pairs[pi]; // *** 2017-12-27 ***
		jsonKey = pairObj->JsonString->Value();
		jsonValue = pairObj->JsonValue->Value();
		ShowMessage(jsonKey + ":" + jsonValue);
	}
    jsonObj->Free();
}
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?