Module: JsonStructure

Defined in:
lib/jsonstructure.rb,
lib/jsonstructure/ffi.rb,
lib/jsonstructure/version.rb,
lib/jsonstructure/binary_installer.rb,
lib/jsonstructure/schema_validator.rb,
lib/jsonstructure/validation_result.rb,
lib/jsonstructure/instance_validator.rb

Overview

JSON Structure SDK for Ruby

This gem provides Ruby bindings to the JSON Structure C library via FFI (Foreign Function Interface). It allows you to validate JSON Structure schemas and validate JSON instances against schemas.

## Thread Safety

This library is thread-safe. Multiple threads can perform validations concurrently without synchronization. The underlying C library uses proper synchronization primitives to protect shared state.

Examples:

Schema Validation

schema = '{"type": "string", "minLength": 1}'
result = JsonStructure::SchemaValidator.validate(schema)
puts "Valid!" if result.valid?

Instance Validation

schema = '{"type": "string"}'
instance = '"hello"'
result = JsonStructure::InstanceValidator.validate(instance, schema)
puts "Valid!" if result.valid?

Concurrent Validation

threads = 10.times.map do |i|
  Thread.new do
    result = JsonStructure::SchemaValidator.validate('{"type": "string"}')
    puts "Thread #{i}: #{result.valid?}"
  end
end
threads.each(&:join)

See Also:

Defined Under Namespace

Modules: FFI Classes: BinaryInstaller, Error, InstanceValidationError, InstanceValidator, SchemaValidationError, SchemaValidator, ValidationError, ValidationResult

Constant Summary collapse

VERSION =

Version of the JSON Structure Ruby SDK

'0.5.5'

Class Method Summary collapse

Class Method Details

.safe_cleanupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Safely clean up the library, waiting for active validations



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jsonstructure.rb', line 87

def safe_cleanup
  @cleanup_mutex.synchronize do
    @shutting_down = true
  end

  # Wait briefly for active validations to complete (up to 1 second)
  10.times do
    break unless validations_active?

    sleep 0.1
  end

  # Perform cleanup - the C library is thread-safe, so this is safe
  # even if a validation is somehow still running
  FFI.js_cleanup
end

.validation_completedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Track when a validation completes



71
72
73
74
75
# File 'lib/jsonstructure.rb', line 71

def validation_completed
  @cleanup_mutex.synchronize do
    @active_validations -= 1
  end
end

.validation_startedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Track when a validation starts



61
62
63
64
65
66
67
# File 'lib/jsonstructure.rb', line 61

def validation_started
  @cleanup_mutex.synchronize do
    raise Error, 'Library is shutting down' if @shutting_down

    @active_validations += 1
  end
end

.validations_active?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if any validations are currently active

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/jsonstructure.rb', line 79

def validations_active?
  @cleanup_mutex.synchronize do
    @active_validations > 0
  end
end