Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String parsing #9

Closed
alexmarchant opened this issue Jun 26, 2014 · 14 comments
Closed

String parsing #9

alexmarchant opened this issue Jun 26, 2014 · 14 comments

Comments

@alexmarchant
Copy link

Cool library!

Having trouble getting the right output from string input:

var json = JSONValue("{\"a\":\"b\"}") // (Enum Value)
json == JSONValue.JInvalid // false
json["a"].string // nil
@delebedev
Copy link

Interesting use case! Why do you expect to achieve from this input, a dictionary, right?
Why don't you specify [a: b] (a dictionary) as an input?

@lingoer
Copy link
Member

lingoer commented Jun 26, 2014

Interesting
@garnett Shall we introduce some LiteralConvertible Protocol or some other init method?

@alexmarchant
Copy link
Author

I was just using it to play around with in a Playground to see how JSONValue's worked. From the source it looked like JSONValue() took String input so I tried it out. Haha, now after reading your reply I get that JSONValue's hold individual element data and that it wasn't meant to parse string input.

Ya, that was confusing to me, i'd recommend making a separate init that only takes NSData or allowing String input as well.

@delebedev
Copy link

I've thought about implementing literalConvertibles, but It's not the case where it will help. If we implement convertible, the call will look like:
let value : JSONValue = [1, :"Foo"]
By manually specifying the type, we allow to assign the value omitting the constructor (now it looks like let value = JSONValue([1, :"Foo"])

@delebedev
Copy link

Ya, that was confusing to me, i'd recommend making a separate init that only takes NSData or allowing String input as well.
Good point! @lingoer we should rethink public API and use cases of the library. Accepting both primitive types and NSData as well really looks confusing.

Maybe, it will be better to make the following public api:

  • fromObject - converts json from NSJSONSerialization result
  • fromData - which internally uses NSJSONSerialization to get object representation.

This will allow us to keep these two use cases separated

@lingoer
Copy link
Member

lingoer commented Jun 26, 2014

Agreed! :)
Yesterday, I've replied a similar question in a Chinese forum with a similar answer to you.
I think a class func parseFromString() is also needed

@delebedev
Copy link

As addition to previous comment: I personally do not like .JInvalid as return value.
let value = JSONValue(...) if value != .JInvalid { ... some logic here }

For me, it looks more natural to return here something like fancy optional wrapper: http://swiftlytyping.tumblr.com/post/88210131086/error-handling
It can return both Error and JSONValue

@delebedev
Copy link

@lingoer it will be really pain to implement, regexp all the things? I still do not see any real-world usage for this

@lingoer
Copy link
Member

lingoer commented Jun 26, 2014

An ugly implementation would be String => NSData => NSJSONSerialization, ugly, but worked?
As for JInvalid :

    case JNumber(Double)
    case JString(String)
    case JBool(Bool)
    case JNull
    case JArray(Array<JSONValue>)
    case JObject(Dictionary<String,JSONValue>)
    case JInvalid(NSError)

@delebedev
Copy link

I like JInvalid(NSError), let's keep this

@lingoer
Copy link
Member

lingoer commented Jun 26, 2014

Ok, I'll commit the JInvalid thing later and think awhile for the init method. I want to keep NSData as the default init ,while provide some other init method for other types

@gcmartijn
Copy link

Could someone give a example how to parse these JSON strings ?

Received_JSON_string: {"a":"actionvalue"}

let json = JSON(Received_JSON_string)
let action = json["a"].stringValue
println("Received a: (action)") // must give me the actionvalue

@lobodart
Copy link

let json: JSON = JSON(data: Received_JSON_string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
let action = json["a"].stringValue
println("Received a: \(action)")

@gcmartijn
Copy link

@lobodart Works !

nghuiqin pushed a commit to nghuiqin/SwiftyJSON that referenced this issue Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants