Skip to content

Commit 44f3dd0

Browse files
committed
Add archiveJSON implementation in Swift
1 parent 900a721 commit 44f3dd0

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

Sources/OpenGraph/Graph/Graph.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Status: WIP
77

88
public import OpenGraph_SPI
9+
import Foundation
910

1011
extension Graph {
1112
public static func typeIndex(
@@ -71,3 +72,27 @@ extension Graph {
7172
@inlinable
7273
public static func setOutputValue<Value>(_ value: UnsafePointer<Value>)
7374
}
75+
76+
extension Graph {
77+
public func archiveJSON(name: String?) {
78+
let options: NSDictionary = [
79+
Graph.descriptionFormat: "graph/dict",
80+
Graph.descriptionIncludeValues: true,
81+
]
82+
guard let description = Graph.description(self, options: options) else { // FIXME
83+
return
84+
}
85+
var name = name ?? "graph"
86+
name.append(".ag-json")
87+
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(name)
88+
guard let data = try? JSONSerialization.data(withJSONObject: description, options: []) else {
89+
return
90+
}
91+
let url = URL(fileURLWithPath: path)
92+
do {
93+
try data.write(to: url, options: [])
94+
print("Wrote graph data to \"\(path)\".")
95+
} catch {
96+
}
97+
}
98+
}

Sources/OpenGraph_SPI/include/OGGraph.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ OG_EXPORT
3737
OG_REFINED_FOR_SWIFT
3838
OGGraphRef OGGraphCreateShared(_Nullable OGGraphRef graph) OG_SWIFT_NAME(OGGraphRef.init(shared:));
3939

40-
OG_EXPORT
41-
OG_REFINED_FOR_SWIFT
42-
void OGGraphArchiveJSON(char const * _Nullable name) OG_SWIFT_NAME(OGGraphRef.archiveJSON(name:));
43-
44-
OG_EXPORT
45-
OG_REFINED_FOR_SWIFT
46-
void OGGraphArchiveJSON2(char const * _Nullable name, uint8_t options) OG_SWIFT_NAME(OGGraphRef.archiveJSON(name:options:));
47-
48-
OG_EXPORT
49-
OG_REFINED_FOR_SWIFT
50-
_Nullable CFTypeRef OGGraphDescription(_Nullable OGGraphRef graph, CFDictionaryRef options) OG_SWIFT_NAME(OGGraphRef.description(_:options:));
51-
5240
OG_EXPORT
5341
OG_REFINED_FOR_SWIFT
5442
CFTypeID OGGraphGetTypeID(void) OG_SWIFT_NAME(getter:OGGraphRef.typeID());

Sources/OpenGraph_SPI/include/OGGraphDescription.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define OGGraphDescription_h
77

88
#include "OGBase.h"
9+
#include "OGGraph.h"
910

1011
OG_ASSUME_NONNULL_BEGIN
1112

@@ -29,6 +30,18 @@ static const CFStringRef OGDescriptionAllGraphs OG_SWIFT_NAME(OGGraphRef.descrip
2930

3031
#endif
3132

33+
OG_EXPORT
34+
OG_REFINED_FOR_SWIFT
35+
void OGGraphArchiveJSON(char const * _Nullable name) OG_SWIFT_NAME(OGGraphRef.archiveJSON(name:));
36+
37+
OG_EXPORT
38+
OG_REFINED_FOR_SWIFT
39+
void OGGraphArchiveJSON2(char const * _Nullable name, uint8_t options) OG_SWIFT_NAME(OGGraphRef.archiveJSON(name:options:));
40+
41+
OG_EXPORT
42+
OG_REFINED_FOR_SWIFT
43+
_Nullable CFTypeRef OGGraphDescription(_Nullable OGGraphRef graph, CFDictionaryRef options) OG_SWIFT_NAME(OGGraphRef.description(_:options:));
44+
3245
OG_EXTERN_C_END
3346

3447
OG_IMPLICIT_BRIDGING_DISABLED

0 commit comments

Comments
 (0)