oj-introspect-0.7.1/0000755000175000017500000000000014353336456013371 5ustar vinayvinayoj-introspect-0.7.1/.rspec0000644000175000017500000000006514353336456014507 0ustar vinayvinay--format documentation --color --require spec_helper oj-introspect-0.7.1/Gemfile0000644000175000017500000000030114353336456014656 0ustar vinayvinaysource "https://rubygems.org" # Specify your gem's dependencies in oj-introspect.gemspec gemspec gem "benchmark-ips" gem "pry" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rake-compiler" oj-introspect-0.7.1/Rakefile0000644000175000017500000000042314353336456015035 0ustar vinayvinayrequire "bundler/gem_tasks" require "rspec/core/rake_task" require "rake/extensiontask" RSpec::Core::RakeTask.new(:spec) task default: [:compile, :spec] Rake::ExtensionTask.new "oj-introspect" do |ext| ext.name = "introspect_ext" ext.lib_dir = "lib/oj/introspect" end oj-introspect-0.7.1/Gemfile.lock0000644000175000017500000000156414353336456015621 0ustar vinayvinayPATH remote: . specs: oj-introspect (0.7.1) oj (>= 3.13.23) GEM remote: https://rubygems.org/ specs: benchmark-ips (2.10.0) coderay (1.1.3) diff-lcs (1.5.0) method_source (1.0.0) oj (3.13.23) pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) rake (13.0.6) rake-compiler (1.2.0) rake rspec (3.12.0) rspec-core (~> 3.12.0) rspec-expectations (~> 3.12.0) rspec-mocks (~> 3.12.0) rspec-core (3.12.0) rspec-support (~> 3.12.0) rspec-expectations (3.12.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-mocks (3.12.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-support (3.12.0) PLATFORMS ruby DEPENDENCIES benchmark-ips oj-introspect! pry rake (~> 13.0) rake-compiler rspec (~> 3.0) BUNDLED WITH 2.1.4 oj-introspect-0.7.1/LICENSE.txt0000644000175000017500000000207314353336456015216 0ustar vinayvinayThe MIT License (MIT) Copyright (c) 2022 Mehmet Emin INAC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. oj-introspect-0.7.1/README.md0000644000175000017500000000345414353336456014656 0ustar vinayvinay# Oj::Introspect Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/oj/introspect`. To experiment with that code, run `bin/console` for an interactive prompt. TODO: Delete this and the text above, and describe your gem ## Installation Add this line to your application's Gemfile: ```ruby gem 'oj-introspect' ``` And then execute: $ bundle install Or install it yourself as: $ gem install oj-introspect ## Usage TODO: Write usage instructions here ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oj-introspect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/oj-introspect/blob/master/CODE_OF_CONDUCT.md). ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct Everyone interacting in the Oj::Introspect project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/oj-introspect/blob/master/CODE_OF_CONDUCT.md). oj-introspect-0.7.1/oj-introspect.gemspec0000644000175000017500000000223414353336456017537 0ustar vinayvinayrequire_relative 'lib/oj/introspect/version' Gem::Specification.new do |spec| spec.name = "oj-introspect" spec.version = Oj::Introspect::VERSION spec.authors = ["Mehmet Emin INAC"] spec.email = ["mehmetemininac@gmail.com"] spec.summary = "Oj introspect parser." spec.description = "Embeds start and end byte offsets of JSON objects into generated Ruby hashes." spec.homepage = "https://github.com/meinac/oj-introspect" spec.license = "MIT" spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0") # spec.metadata["homepage_uri"] = spec.homepage # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.extensions = ["ext/oj-introspect/extconf.rb"] spec.add_dependency "oj", ">=3.13.23" end oj-introspect-0.7.1/ext/0000755000175000017500000000000014353336456014171 5ustar vinayvinayoj-introspect-0.7.1/ext/oj-introspect/0000755000175000017500000000000014353336456016771 5ustar vinayvinayoj-introspect-0.7.1/ext/oj-introspect/introspect.c0000644000175000017500000002070614353336456021334 0ustar vinayvinay#include #include "oj.h" #include "parser.h" #include "usual.h" #define BYTE_OFFSETS_STACK_INC_SIZE 256 // Holds the start byte offsets of each JSON object encountered struct _byte_offsets { int length; int current; long *stack; }; typedef struct _introspect_S { struct _usual usual; // inherit all the attributes from `_usual` struct struct _byte_offsets byte_offsets; // I think it's better to encapsulate common fields under a namespace char *filter; bool introspect; void (*delegated_start_func)(struct _ojParser *p); void (*delegated_free_func)(struct _ojParser *p); VALUE (*delegated_option_func)(struct _ojParser *p, const char *key, VALUE value); void (*delegated_open_object_func)(struct _ojParser *p); void (*delegated_open_object_key_func)(struct _ojParser *p); void (*delegated_open_array_key_func)(struct _ojParser *p); void (*delegated_close_object_func)(struct _ojParser *p); void (*delegated_close_object_key_func)(struct _ojParser *p); void (*delegated_close_array_key_func)(struct _ojParser *p); } * IntrospectDelegate; static VALUE introspection_key; static VALUE start_byte_key; static VALUE end_byte_key; static void dfree(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; if(d->filter != NULL) xfree(d->filter); xfree(d->byte_offsets.stack); d->delegated_free_func(p); } static void start(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_start_func(p); // Reset to zero so the parser and delegate can be reused. d->byte_offsets.current = 0; /* * If the `filter` is provided we will start introspecting later * once we encounter with the key provided. */ d->introspect = (d->filter == NULL); } static void copy_ruby_str(char **target, VALUE source) { const char *ruby_str = StringValueCStr(source); size_t len = strlen(ruby_str); *target = ALLOC_N(char, len + 1); strncpy(*target, ruby_str, len + 1); } static VALUE option(ojParser p, const char *key, VALUE value) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; if(strcmp(key, "filter=") == 0) { Check_Type(value, T_STRING); copy_ruby_str(&d->filter, value); // We need to copy the value as GC can free it later. return Qtrue; } return d->delegated_option_func(p, key, value); } static void ensure_byte_offsets_stack(IntrospectDelegate d) { if (RB_UNLIKELY(d->byte_offsets.current == (d->byte_offsets.length - 1))) { d->byte_offsets.length += BYTE_OFFSETS_STACK_INC_SIZE; REALLOC_N(d->byte_offsets.stack, long, d->byte_offsets.length + BYTE_OFFSETS_STACK_INC_SIZE); } } static long pop(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; return d->byte_offsets.stack[--d->byte_offsets.current]; } static void push(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; ensure_byte_offsets_stack(d); d->byte_offsets.stack[d->byte_offsets.current++] = p->cur; } static void open_object_introspected(ojParser p) { push(p); IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_open_object_func(p); } static char * previously_inserted_key(IntrospectDelegate d) { Key key = (d->usual.ktail - 1); return ((size_t)key->len < sizeof(key->buf)) ? key->buf : key->key; } static bool should_switch_introspection(IntrospectDelegate d) { return strcmp(d->filter, previously_inserted_key(d)) == 0; } /* * WHEN there is a filter * AND * WHEN the introspection is disabled * AND the last inserted key matches the filter * THEN enable introspection * OR * WHEN the introspection is enabled * AND the last inserted key matches the filter * THEN disable introspection */ static void switch_introspection(IntrospectDelegate d) { if(d->filter == NULL) return; d->introspect = should_switch_introspection(d) != d->introspect; // a XOR b } static void open_object_key_introspected(ojParser p) { push(p); IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_open_object_key_func(p); if(!d->introspect) switch_introspection(d); } static void open_array_key_introspected(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_open_array_key_func(p); if(!d->introspect) switch_introspection(d); } static void set_introspection_values(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; if(!d->introspect) return; volatile VALUE obj = rb_hash_new(); rb_hash_aset(obj, start_byte_key, INT2FIX(pop(p))); rb_hash_aset(obj, end_byte_key, INT2FIX(p->cur)); rb_hash_aset(*(d->usual.vtail - 1), introspection_key, obj); } static void close_object_introspected(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_close_object_func(p); set_introspection_values(p); } // We switch introspection off only for object and array keys. static void close_object_key_introspected(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_close_object_func(p); if(d->introspect) { set_introspection_values(p); switch_introspection(d); } } static void close_array_key_introspected(ojParser p) { IntrospectDelegate d = (IntrospectDelegate)p->ctx; d->delegated_close_array_key_func(p); if(d->introspect) switch_introspection(d); } static void init_introspect_parser(ojParser p, VALUE ropts) { IntrospectDelegate d = ALLOC(struct _introspect_S); oj_init_usual(p, &d->usual); // now function mangling... d->delegated_free_func = p->free; p->free = &dfree; d->delegated_start_func = p->start; p->start = start; d->delegated_option_func = p->option; p->option = option; // Wrap original functions to collect byte offsets Funcs f = &p->funcs[TOP_FUN]; d->delegated_open_object_func = f->open_object; d->delegated_close_object_func = f->close_object; f->open_object = open_object_introspected; f->close_object = close_object_introspected; f = &p->funcs[ARRAY_FUN]; f->open_object = open_object_introspected; f->close_object = close_object_introspected; f = &p->funcs[OBJECT_FUN]; d->delegated_open_array_key_func = f->open_array; d->delegated_open_object_key_func = f->open_object; d->delegated_close_array_key_func = f->close_array; d->delegated_close_object_key_func = f->close_object; f->open_array = open_array_key_introspected; f->open_object = open_object_key_introspected; f->close_array = close_array_key_introspected; f->close_object = close_object_key_introspected; // Init stack d->byte_offsets.current = 0; d->byte_offsets.stack = ALLOC_N(long, BYTE_OFFSETS_STACK_INC_SIZE); d->byte_offsets.length = BYTE_OFFSETS_STACK_INC_SIZE; d->filter = NULL; // Process options. oj_parser_set_option(p, ropts); } static VALUE rb_new_introspect_parser(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 1); VALUE options; if(argc == 1) { options = argv[0]; Check_Type(options, T_HASH); } else { options = rb_hash_new(); } VALUE oj_parser = oj_parser_new(); struct _ojParser *p; Data_Get_Struct(oj_parser, struct _ojParser, p); init_introspect_parser(p, options); return oj_parser; } // This code is neither Ruby Thread safe nor Ractor safe! // I don't really want to write a mutex right now. static VALUE rb_get_default_introspect_parser(VALUE self) { VALUE current_thread = rb_funcall(rb_cThread, rb_intern("current"), 0); VALUE thread_parser = rb_funcall(current_thread, rb_intern("[]"), 1, rb_str_new_literal("oj_introspect_parser")); if(RTEST(thread_parser)) return thread_parser; thread_parser = rb_new_introspect_parser(0, NULL, self); rb_funcall(current_thread, rb_intern("[]="), 2, rb_str_new_literal("oj_introspect_parser"), thread_parser); return thread_parser; } void Init_introspect_ext() { VALUE oj_module = rb_const_get(rb_cObject, rb_intern("Oj")); VALUE parser_module = rb_const_get(oj_module, rb_intern("Parser")); VALUE introspection_class = rb_define_class_under(oj_module, "Introspect", rb_cObject); introspection_key = ID2SYM(rb_intern("__oj_introspection")); rb_gc_register_address(&introspection_key); start_byte_key = ID2SYM(rb_intern("start_byte")); rb_gc_register_address(&start_byte_key); end_byte_key = ID2SYM(rb_intern("end_byte")); rb_gc_register_address(&end_byte_key); rb_const_set(introspection_class, rb_intern("KEY"), introspection_key); rb_define_singleton_method(parser_module, "introspect", rb_get_default_introspect_parser, 0); rb_define_singleton_method(introspection_class, "new", rb_new_introspect_parser, -1); } oj-introspect-0.7.1/ext/oj-introspect/oj.sym0000644000175000017500000000006414353336456020133 0ustar vinayvinay_oj_init_usual _oj_parser_new _oj_parser_set_option oj-introspect-0.7.1/ext/oj-introspect/extconf.rb0000644000175000017500000000160314353336456020764 0ustar vinayvinayrequire "mkmf" require "pathname" require "oj" oj_version_file = Oj.const_source_location(:VERSION).first oj_version_file_path = Pathname.new(oj_version_file) OJ_HEADERS = oj_version_file_path.join('..', '..', '..', 'ext', 'oj').to_s cc_version = `#{RbConfig.expand("$(CC) --version".dup)}` if cc_version.match?(/clang/i) # Ignore symbols loaded from Oj in case Ruby is compiled without # "-Wl,-undefined,dynamic_lookup" (related to https://bugs.ruby-lang.org/issues/19005) symfile = File.join(__dir__, 'oj.sym') dynamic_symbols = File.readlines(symfile) dynamic_symbols.each do |sym| $DLDFLAGS << " -Wl,-U,#{sym.strip}" end # Needed for Ruby 3.2 ABI check: https://github.com/ruby/ruby/pull/5474 if RUBY_VERSION >= "3.2" $LDFLAGS << " -Wl,-exported_symbol,_ruby_abi_version" end end dir_config('oj', [OJ_HEADERS], []) create_makefile("oj/introspect/introspect_ext") oj-introspect-0.7.1/bin/0000755000175000017500000000000014353336456014141 5ustar vinayvinayoj-introspect-0.7.1/bin/benchmark0000755000175000017500000000120714353336456016021 0ustar vinayvinay#!/usr/bin/env ruby require "bundler/setup" require "oj/introspect" require "json" require "benchmark/ips" TEST_JSON = File.read("./spec/fixtures/vulnerabilities.json") INTROSPECT_PARSER = Oj::Parser.introspect FILTERED_INTROSPECT_PARSER = Oj::Introspect.new(filter: "remediations") USUAL_PARSER = Oj::Parser.usual Benchmark.ips do |b| b.report("Introspect") do INTROSPECT_PARSER.parse(TEST_JSON) end b.report("Filtered introspect") do FILTERED_INTROSPECT_PARSER.parse(TEST_JSON) end b.report("Oj usual") do USUAL_PARSER.parse(TEST_JSON) end b.report("stdlib") do JSON.parse(TEST_JSON) end b.compare! end oj-introspect-0.7.1/bin/console0000755000175000017500000000053414353336456015533 0ustar vinayvinay#!/usr/bin/env ruby require "bundler/setup" require "oj/introspect" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require "irb" IRB.start(__FILE__) oj-introspect-0.7.1/bin/test_run0000755000175000017500000000022214353336456015726 0ustar vinayvinay#!/usr/bin/env ruby require "bundler/setup" require "oj/introspect" TEST_JSON = '{ "foo": "bar" }' puts Oj::Parser.introspect.parse(TEST_JSON) oj-introspect-0.7.1/bin/setup0000755000175000017500000000020314353336456015222 0ustar vinayvinay#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx bundle install # Do any other automated setup that you need to do here oj-introspect-0.7.1/CODE_OF_CONDUCT.md0000644000175000017500000000624314353336456016175 0ustar vinayvinay# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at mehmetemininac@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [https://contributor-covenant.org/version/1/4][version] [homepage]: https://contributor-covenant.org [version]: https://contributor-covenant.org/version/1/4/ oj-introspect-0.7.1/lib/0000755000175000017500000000000014353336456014137 5ustar vinayvinayoj-introspect-0.7.1/lib/oj/0000755000175000017500000000000014353336456014547 5ustar vinayvinayoj-introspect-0.7.1/lib/oj/introspect/0000755000175000017500000000000014353336456016741 5ustar vinayvinayoj-introspect-0.7.1/lib/oj/introspect/version.rb0000644000175000017500000000007514353336456020755 0ustar vinayvinaymodule Oj class Introspect VERSION = "0.7.1" end end oj-introspect-0.7.1/lib/oj/introspect.rb0000644000175000017500000000024114353336456017263 0ustar vinayvinayrequire "oj" require "oj/introspect/version" require "oj/introspect/introspect_ext" module Oj class Introspect class Error < StandardError; end end end oj-introspect-0.7.1/.gitignore0000644000175000017500000000021514353336456015357 0ustar vinayvinay/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status *.bundle Makefile *.o *.so