Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/protocol/websocket/coder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module WebSocket
# @namespace
module Coder
# The default coder for WebSocket messages.
DEFAULT = JSON::DEFAULT
DEFAULT = JSON
end
end
end
19 changes: 5 additions & 14 deletions lib/protocol/websocket/coder/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@ module Protocol
module WebSocket
module Coder
# A JSON coder that uses the standard JSON library.
class JSON
# Initialize a new JSON coder.
# @parameter options [Hash] Options to pass to the JSON library when parsing or generating.
def initialize(**options)
@options = options
end

module JSON
# Parse a JSON buffer into an object.
def parse(buffer)
::JSON.parse(buffer, **@options)
def self.parse(buffer)
::JSON.parse(buffer, symbolize_names: true)
end

# Generate a JSON buffer from an object.
def generate(object)
::JSON.generate(object, **@options)
def self.generate(object)
::JSON.generate(object)
end

# The default JSON coder. This coder will symbolize names.
DEFAULT = new(symbolize_names: true)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Unreleased

- Fix JSON 3 compatibility by applying `symbolize_names` only when parsing incoming messages. `Coder::JSON` is now a module with fixed parsing and generation behavior; pass a different coder to customize serialization.

## v0.21.1

- If `Connection#close_write` fails, the connection will now be fully closed to prevent hanging connections.
Expand Down
19 changes: 19 additions & 0 deletions test/protocol/websocket/coder/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2026, by Samuel Williams.

require "protocol/websocket/coder/json"

describe Protocol::WebSocket::Coder::JSON do
let(:buffer) {'{"hello":"world"}'}
let(:object) {{hello: "world"}}

it "parses object keys as symbols" do
expect(subject.parse(buffer)).to be == object
end

it "generates JSON" do
expect(subject.generate(object)).to be == buffer
end
end
Loading