D言語からObjective-CとCocoaAPIを使いたい
JSONから NSDictionary、NSArray に
JSONのデータ形式から、NSDictionary の配列 NSArray に変換してみました。
サンプルコード
Powered by Prism
dicjson/dub.json
{
"authors": ["hidhok"],
"description": "A D/Objective-C Cocoa application.",
"name": "dicjson",
"dflags": ["-L-framework", "-LCocoa"],
"sourceFiles": ["../cocoa/strings"]
}
dicjson/source/app.d
/**
* dicjson/source/app.d Version 0.0.1
*/
module dicjson.main;
import core.attribute : selector;
import std.stdio;
import cocoa.strings;
void main(string[] args) {
string mlbStrPath = "./mlb.json";
NSArray mlbArray = mlbStrPath.toArrayJson;
writeln(" ------- Team ------- Division - League");
foreach (i; 0 .. mlbArray.count) {
NSDictionary mlbDic = mlbArray.dicAtIndex(i);
writefln(" %2d %-22s %-7s %-8s", i + 1,
mlbDic.key("Team".toNs).toDs,
mlbDic.key("Division".toNs).toDs,
mlbDic.key("League".toNs).toDs);
}
}
NSArray toArrayJson(string path) {
NSData jStrData = NSData.data(path.toNs);
NSArray jArray = NSJSONSerialization.toJsArray(
jStrData, NSJSONReading.MutableContainers, null);
return jArray;
}
NSDictionary toDicJson(string path) {
NSData jStrData = NSData.data(path.toNs);
NSDictionary jDic = NSJSONSerialization.toJsDic(
jStrData, NSJSONReading.MutableContainers, null);
return jDic;
}
extern (Objective-C)
extern class NSJSONSerialization : NSObject {
// Creating JSON Object from JSON data
static NSObject toJsObj(NSData, NSJSONReading, NSError)
@selector("JSONObjectWithData:options:error:");
static NSDictionary toJsDic(NSData, NSJSONReading, NSError)
@selector("JSONObjectWithData:options:error:");
static NSArray toJsArray(NSData, NSJSONReading, NSError)
@selector("JSONObjectWithData:options:error:");
static NSObject toJsObj(NSInputStream, NSJSONReading, NSError)
@selector("JSONObjectWithStream:options:error:");
// Creating JSON data from JSON Object
static NSData toJsData(NSObject, NSJSONWriting, NSError)
@selector("dataWithJSONObject:options:error:");
static NSInteger writeJsObj(NSObject, NSOutputStream, NSJSONWriting, NSError)
@selector("writeJSONObject:toStream:options:error:");
}
enum NSJSONReading : NSUInteger { // NSJSONReadingOptions
MutableContainers = 1UL << 0, // 返された配列・辞書が変更可能
MutableLeaves = 1UL << 1, // グラフ内のリーフ文字列が変更可能
FragmentsAllowed = 1UL << 2, // オブジェクトの許可
JSON5Allowed = 1UL << 3, // JSON5構文をサポート
TopLevelDictionaryAssumed = 1UL << 4 // 辞書の想定を指定
}
enum NSJSONWriting : NSUInteger { // NSJSONWritingOptions
PrettyPrinted = 1UL << 0, // 空白・インデントを使用
SortedKeys = 1UL << 1, // キーを辞書順に並べ替え
FragmentsAllowed = 1UL << 2, // 配列・辞書でないオブジェクトを許可
WithoutEscapingSlashes = 1UL << 3 // スラッシュ文字の前にエスケープ文字を付けない
}
extern (Objective-C)
extern class NSData : NSObject {
// Creating Data
static NSData data() @selector("data");
static NSData bytes(void*, NSUInteger) @selector("dataWithBytes:length:");
static NSData noCopy(void*, NSUInteger) @selector("dataWithBytesNoCopy:length:");
static NSData noCopy(void*, NSUInteger, bool)
@selector("dataWithBytesNoCopy:length:freeWhenDone:");
static NSData data(NSData) @selector("dataWithData:");
NSData init(void*, NSUInteger) @selector("initWithBytes:length:");
NSData initNoCopy(void*, NSUInteger) @selector("initWithBytesNoCopy:length:");
NSData init(NSData) @selector("initWithData:");
// Reading Data from a File
static NSData data(NSString path) @selector("dataWithContentsOfFile:");
static NSData data(NSString, NSDataReading, NSError)
@selector("dataWithContentsOfFile:options:NSError:");
static NSData data(NSURL url) @selector("dataWithContentsOfURL:");
// Writing Data to File
NSData writeFile(NSString path, bool) @selector("writeToFiel:atomically:");
NSData writeFile(NSString, NSDataWriting, NSError)
@selector("writeToFile:options:error:");
}
enum NSDataReading : NSUInteger { // NSDataReadingOptions
MappedIfSafe = 1UL << 0, // ファイルを仮想メモリにマップ
Uncached = 1UL << 1, // キャッシュに保存すべきでない
MappedAlways = 1UL << 3 // ファイルをマップする
}
enum NSDataWriting : NSUInteger {
Atomic = 1UL << 0, // 補助ファイル使用
WithoutOverwriting = 1UL << 1, // ファイルが存在する時はエラー
FileProtectionNone = 0x10000000,
ProtectNone = 0x10000000, // 暗号化しない
FileProtectionComplete = 0x20000000,
ProtectComplete = 0x20000000, // ロックが解除されている間のみ
FileProtectionCompleteUnlessOpen = 0x30000000,
ProtectUnlessOpen = 0x30000000, // ロック解除または開いているとき
FileProtectionCompleteUntilFirstUserAuthentication = 0x40000000,
ProtectFirstUse = 0x40000000, // 最初に解除したとき
FileProtectionMask = 0xf0000000 // システムが使用
}
extern (Objective-C)
extern class NSInputStream : NSStream {}
extern (Objective-C)
extern class NSOutputStream : NSStream {}
extern (Objective-C)
extern class NSStream : NSObject {}
Terminal
(base) hidhok dicjson % dub
Starting Performing "debug" build using /Library/D/dmd/bin/dmd for x86_64.
Building dicjson ~master: building configuration [application]
Linking dicjson
Running dicjson
------- Team ------- Division - League
1 Arizona Diamondbacks West National
2 Atlanta Braves East National
3 Baltimore Orioles East American
4 Boston Red Sox East American
5 Chicago Cubs Central National
6 Chicago White Sox Central American
7 Cincinnati Reds Central National
8 Cleveland Guardians Central American
9 Colorado Rockies West National
10 Detroit Tigers Central American
11 Houston Astros West American
12 Kansas City Royals Central American
13 Los Angeles Angels West American
14 Los Angeles Dodgers West National
15 Miami Marlins East National
16 Milwaukee Brewers Central National
17 Minnesota Twins Central American
18 New York Mets East National
19 New York Yankees East American
20 Oakland Athletics West American
21 Philadelphia Phillies East National
22 Pittsburgh Pirates Central National
23 San Diego Padres West National
24 San Francisco Giants West National
25 Seattle Mariners West American
26 St. Louis Cardinals Central National
27 Tampa Bay Rays East American
28 Texas Rangers West American
29 Toronto Blue Jays East American
30 WashingTon Nationals East National
(base) hidhok dicjson %
mlb.json
cocoa/strings.d
- Log
- 2024-06-08 修正しました
- 2023-10-07 upload