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

JSON array to String #79

Closed
radianttap opened this issue Nov 14, 2016 · 4 comments
Closed

JSON array to String #79

radianttap opened this issue Nov 14, 2016 · 4 comments
Labels

Comments

@radianttap
Copy link
Contributor

How can Marshal be extended to allow converting a JSON like this:

"somekey": ["s1", "s2", "s3"]

into this

"s1,s2,s3"

I had few multiple false starts already, can't figure it out.

@jarsen
Copy link
Member

jarsen commented Nov 15, 2016

Do you have a specific reason for wanting to extend Marshal in this case?

Could you just do something like this?

let strings: [String] = try json.value(for: "somekey")
let joinedString = strings.joined(separator: ",")

@radianttap
Copy link
Contributor Author

Thanks! This will do as shortcut.

The reason I wanted to extend it is to automate generation of the update() calls for each entity in my data model:

    public func update(object: MarshaledObject, inContext context: NSManagedObjectContext) throws {

        if let key = NavigationNode.jsonKeys["childNodesCount"] { childNodesCount = try object.value(for: key) }
...

(jsonKeys maps from coredata attributes to whatever is coming from JSON).

I am using custom mogenerator templates for this and it would be pretty unwieldy pretty soon if I start adding this:

if let key = NavigationNode.jsonKeys["csvNavigationTypes"] {
    let strings: [String] = try object.value(for: key)
    csvNavigationTypes = strings.joined(separator: ",")
}

as exceptions. I can do post-generation changes like above, only need to remember doing them each time model classes are regenerated. Not a big issue for this particular model, but would like a cleaner solution.

I also auto-generate marshaled() methods so would need the opposite direction too.

@jarsen
Copy link
Member

jarsen commented Nov 15, 2016

You might consider then writing reusable functions for transforming to CSV and back.

I think the difficulty here is how do you extend the behavior to provide this behavior for only a subset of [String].

We have been discussing adding an optional transform function to the API in #73. This could help with this as well since you could do something like:

let csv = try json.value(for: "somekey" transform: arrayToCSV)

where csvTransformer is some function that joins your [String] -> String

When marshaling, you'd still just write a csvToArray type function which you call on your csv value.

@radianttap
Copy link
Contributor Author

Thanks, the transform approach sounds really good. I'll keep my eye on the #73 then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants