Re: Writing, then reading JSON
Re: Writing, then reading JSON
- Subject: Re: Writing, then reading JSON
- From: Charles Jenkins <email@hidden>
- Date: Sun, 21 Sep 2014 19:10:37 -0400
Just as a test, I changed my writer method to immediately try to interpret the JSON data and reconstitute the project’s data:
let structureDict = theProject.getStructureDictionary()
let jsonData = NSJSONSerialization.dataWithJSONObject( structureDict, options: nil, error: outError )
if let reconstitutedStructureDict = NSJSONSerialization.JSONObjectWithData( jsonData!, options: nil, error: outError ) as? NSDictionary {
var reconstitutedProj = DocumentNode.readFromWrapperViaStructureDictionary( parentWrapper: theFileWrapper!, dictionary: reconstitutedStructureDict )
}
All this works: reconstitutedStructureDict is a copy of structureDict, and reconstitutedProj is a copy of theProject. And the JSON file that gets written out appears okay, so my problem seems to be that NSFileWrapper.regularFileContents isn’t returning usable data.
—
Charles Jenkins
On Friday, September 19, 2014 at 11:45 AM, Charles Jenkins wrote:
> My document structure is a file wrapper containing a bunch of RTF documents and a file called structure.json which describes how they relate to one another.
>
> I write out the structure file like this:
>
> let structureDict = theProject.getStructureDictionary()
> let jsonData = NSJSONSerialization.dataWithJSONObject( structureDict, options: nil, error: outError )
>
> writeFileToWrapper(
> parentWrapper: theFileWrapper!,
> filename: structureFileName,
> data: jsonData,
> err: outError
> );
>
>
> I’m not including the bodies of getStructureDictionary() or writeFileToWrapper() because they seem to work just fine. The structure.json file appears in my output package, and if I open it using TextWrangler, I see exactly the JSON content I expect, stored in UTF8 encoding.
>
> The thing is, my app can’t read it back in. Here’s the function that’s not working:
>
> override func readFromFileWrapper(
> parentWrapper: NSFileWrapper!,
> ofType typeName: String!,
> error outError: NSErrorPointer
> ) -> Bool
> {
> if let fw = parentWrapper.fileWrappers[ structureFileName ] as? NSFileWrapper {
> if let data = fw.regularFileContents? {
> let debug: String = NSString( data: data, encoding: NSUTF8StringEncoding )
> let obj: AnyObject? = NSJSONSerialization.JSONObjectWithData( data, options: nil, error: outError )
> if let structureDict = obj as? NSDictionary {
> var proj = DocumentNode.readFromWrapperViaStructureDictionary( parentWrapper: parentWrapper, dictionary: structureDict )
> theProject = proj
> theFileWrapper = parentWrapper
> return true
> }
> }
> }
> return false;
> }
>
>
> I expect I’ll find bugs in readFromWrapperViaStructureDictionary() if I ever call it, but I never get that far.
>
> With Swift and Xcode, stepping line-by-line through code it a bit confusing because the current line indicator bounces all around, sometimes appearing on lines of code already executed. But to be best of my belief, my problem is that obj can’t be converted to an NSDictionary. I inserted the debug: String to see what’s read from the file, and it comes back as garbage.
>
> Is calling regularFileContents the wrong way to read up my JSON file?
>
> —
>
> Charles Jenkins
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden