Giter Site home page Giter Site logo

swift-purejsonserializer's Introduction

PureJsonSerializer Build Status Carthage compatible

A pure-Swift JSON serializer and deserializer.

COCOAPODS

Lot's of CocoaPods means lots of failed namespaces. The actual pod for this library is called PureJsonSerializer.

pod 'PureJsonSerializer'

DESERIALIZE

import PureJsonSerializer

// parse a JSON data
let data: NSData = ...

do {
  let json = try Json.deserialize(jsonSource)
  let value = json["Foo"]?["bar"]?.stringValue ?? ""
  print(value)
} catch {
  print("Json serialization failed with error: \(error)")
}

BUILD

// build a JSON structure
let profile: Json = [
  "name": "Swift",
  "started": 2014,
  "keywords": ["OOP", "functional programming", "static types", "iOS"],
]
println(profile.description)      // packed JSON string
println(profile.debugDescription) // pretty JSON string

SERIALIZE

let serializedJson = json.serialize(.PrettyPrint)

DESCRIPTION

Swift-JsonSerializer is a JSON serializer and deserializer which are implemented in Pure Swift and adds nothing to built-in / standard classes in Swift.

GENOME

This library is featured in a complete Json mapping library you can find here.

KNOWN ISSUES

  • This library doesn't work with optimization flags (swiftc -O) as of Xcode 6.1.1 / Swift version 1.1 (swift-600.0.56.1).

SEE ALSO

AUTHOR

Fuji, Goro (gfx) [email protected]

LICENSE

The Apache 2.0 License

swift-purejsonserializer's People

Contributors

edjiang avatar gfx avatar jdg avatar loganwright avatar ninoscript avatar wanewang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-purejsonserializer's Issues

Swift 3 -- Update Pod

The current version of the repo in github seems to work correctly with Swift 3, but the pod itself has not been updated.

Swift 3

Swift 3 changed the stdlib api and some things on how the core language works. I made a fork and updated Swift-PureJsonSerializer so it compiles on the latest snapshot.

Not submitting a pull request since I didn't leave Swift 2.2 compatibility, and it seems like you're distributing on more than just SPM.

Here's a link for reference: https://github.com/edjiang/Swift-PureJsonSerializer (tag 2.0)

Issue when deserializing unicode characters

I'm not sure if there is a standard for Unicode Escape Formats or not, neither do I know if this is a responsibility of PureJsonSerializer, but here I come anyway:

I have a string that's being sent from a backend in ruby, that has characters like this: \u00e9
The parseEscapedChar function is assuming the length of such string is anything between 2 and 8 bytes, and ends when a character that can't represent a hex value is found.

So this: \u00e9r is understood as \u{00e9}r (or ér)
but this: \u00e9d is understood as \u{00e9d} (or ) instead of the expected \u{00e9}d (or éd)

I'm currently doing a preprocessing step to replace these kinds of characters \\u([0-9a-f]{4}) with the actual character before feeding it to PureJsonSerializer.

Here's my current code for that preprocessing step in case it's of any use.

private static func deserializeJsonData(data: NSData) throws -> Json {
    let stringData = try String(data: data, encoding: NSUTF8StringEncoding).unwrap()
    // note: unwrap() is a simple extension to optionals that returns the unwrapped value or throws

    let regex = try NSRegularExpression(pattern: "\\\\u([0-9a-f]{4})", options: .CaseInsensitive)
    let wholeRange = NSMakeRange(0, stringData.characters.count)

    let s = NSMutableString(string: stringData)
    var offset = 0
    regex.enumerateMatchesInString(stringData, options: .WithTransparentBounds, range: wholeRange) { (textCheckingResult, _, _) -> Void in
        guard let result = textCheckingResult else {
            return
        }

        var captureRange = result.rangeAtIndex(1)
        captureRange.location += offset

        var replacementRange = result.range
        replacementRange.location += offset

        let capture = s.substringWithRange(captureRange)
        guard let unicode = Int32(capture, radix: 16) else {
            return
        }

        let replacement = String(format: "%C", unicode)
        s.replaceCharactersInRange(replacementRange, withString: replacement)
        offset += replacement.characters.count - replacementRange.length
    }

    let preprocessedString = String(s)
    return try Json.deserialize(preprocessedString)
}

It's supposed to replace calls to try Json.deserialize(data) in my code.

Swift 3 update

Hey, great little library. Any idea if / when a compatible swift 3 will be released? The lw/swift-3 branch doesn't work for me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.