Skip to content

Conversation

@mattt
Copy link
Contributor

@mattt mattt commented Jan 8, 2026

Hello, @ibireme. Thank you for your work on yyjson!

I recently learned about your project from a PR by @DePasqualeOrg to swift-transformers (huggingface/swift-transformers#304). That led me to creating a Swift package to wrapping it here: https://github.com/mattt/swift-yyjson

In contrast to the other Swift projects already mentioned in the README, this package provides API-compatible replacements for standard Swift APIs, and uses package trait equivalents for compile-time optimizations. 1

Hoping to add benchmarks and more comprehensive testing soon. But wanted to share this with you to get your thoughts and give it more visibility to anyone else who's using yyjson in their apps.

Footnotes

  1. Currently, swift-yyjson vendors C code instead of declaring yyjson as a dependency. While that is possible because yyjson contains a root-level Package.swift file (add swift package manager support #11), that package manifest file doesn't include traits that allow for compile-time options. Let me know if you'd be interested in a PR implementing that! (It should be possible without impacting existing compatibility)

@ibireme ibireme merged commit 22aa3a2 into ibireme:master Jan 9, 2026
@ibireme
Copy link
Owner

ibireme commented Jan 9, 2026

Hi @mattt, I’m glad yyjson has been useful.
Traits and compile-time options in Package.swift sound great, and PRs are welcome.

I took a quick look at the Swift package. There are a couple of small changes that might slightly improve performance.

  1. yyjson_val already stores the string length, so passing it through avoids an extra strlen call. For example:
@inline(__always)
func yyToString(_ val: UnsafeMutablePointer<yyjson_val>) -> String {
    let ptr = unsafe_yyjson_get_str(val)!
    let len = unsafe_yyjson_get_len(val)
    let buf = UnsafeBufferPointer(start: UnsafeRawPointer(ptr).assumingMemoryBound(to: UInt8.self), count: len)
    return String(decoding: buf, as: UTF8.self)
}

@inline(__always)
func yyFromString(_ string: String, in doc: UnsafeMutablePointer<yyjson_mut_doc>) 
    -> UnsafeMutablePointer<yyjson_mut_val>? {
    var tmp = string
    return tmp.withUTF8 { buf in
        guard let ptr = buf.baseAddress else { return nil }
        return yyjson_mut_strncpy(doc, ptr, buf.count)
    }
}
  1. This function can be simplified to just return yyjson_arr_size(value):
    https://github.com/mattt/swift-yyjson/blob/d804b6bfbb23b6f197aa5bd9852fc334fecba744/Sources/YYJSON/Decoder.swift#L970-L977

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

Successfully merging this pull request may close these issues.

2 participants