Module: SmarterJSON::Recovery

Includes:
Bytes
Defined in:
lib/smarter_json/parser.rb

Constant Summary

Constants included from Bytes

Bytes::BACKSLASH, Bytes::COLON, Bytes::COMMA, Bytes::CR, Bytes::DOLLAR, Bytes::DOT, Bytes::DQUOTE, Bytes::HASH, Bytes::LBRACE, Bytes::LBRACKET, Bytes::LF, Bytes::LOWER_E, Bytes::LOWER_F, Bytes::LOWER_N, Bytes::LOWER_T, Bytes::LOWER_U, Bytes::LOWER_X, Bytes::MINUS, Bytes::NINE, Bytes::PLUS, Bytes::RBRACE, Bytes::RBRACKET, Bytes::SLASH, Bytes::SPACE, Bytes::SQUOTE, Bytes::STAR, Bytes::TAB, Bytes::UNDERSCORE, Bytes::UPPER_E, Bytes::UPPER_F, Bytes::UPPER_I, Bytes::UPPER_N, Bytes::UPPER_T, Bytes::UPPER_X, Bytes::ZERO

Class Method Summary collapse

Class Method Details

.candidate_ranges(input) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/smarter_json/parser.rb', line 494

def candidate_ranges(input)
  ranges = []
  stack = []
  start_pos = nil
  i = 0
  mode = nil
  while i < input.bytesize
    b = input.getbyte(i)
    if mode == :double
      if b == BACKSLASH
        i += 2
        next
      elsif b == DQUOTE
        mode = nil
      end
      i += 1
      next
    elsif mode == :single
      if b == BACKSLASH
        i += 2
        next
      elsif b == SQUOTE
        mode = nil
      end
      i += 1
      next
    elsif mode == :triple
      if input.byteslice(i, 3) == "'''"
        mode = nil
        i += 3
      else
        i += 1
      end
      next
    elsif mode == :line_comment
      if [LF, CR].include?(b)
        mode = nil
      else
        i += 1
        next
      end
    elsif mode == :block_comment
      if input.byteslice(i, 2) == "*/"
        mode = nil
        i += 2
      else
        i += 1
      end
      next
    else
      if input.byteslice(i, 2) == "//"
        mode = :line_comment
        i += 2
        next
      elsif input.byteslice(i, 2) == "/*"
        mode = :block_comment
        i += 2
        next
      elsif b == HASH
        mode = :line_comment
        i += 1
        next
      elsif b == DQUOTE
        mode = :double
        i += 1
        next
      elsif input.byteslice(i, 3) == "'''"
        mode = :triple
        i += 3
        next
      elsif b == SQUOTE
        mode = :single
        i += 1
        next
      elsif [LBRACE, LBRACKET].include?(b)
        start_pos = i if stack.empty?
        stack << b
      elsif b == RBRACE
        stack.pop if stack.last == LBRACE
        if stack.empty? && start_pos
          ranges << (start_pos...(i + 1))
          start_pos = nil
        end
      elsif b == RBRACKET
        stack.pop if stack.last == LBRACKET
        if stack.empty? && start_pos
          ranges << (start_pos...(i + 1))
          start_pos = nil
        end
      end
    end
    i += 1
  end
  ranges
end

.emit_wrapper_warnings(payloads, handler) ⇒ Object



399
400
401
402
403
404
405
406
407
# File 'lib/smarter_json/parser.rb', line 399

def emit_wrapper_warnings(payloads, handler)
  return unless handler

  meta = payloads.first[:meta]
  warn(handler, :prefix_text_ignored, "ignored non-JSON text before the payload", *meta[:first_pos]) if meta[:prefix]
  warn(handler, :code_fence_stripped, "stripped markdown code fences around the payload", *meta[:first_pos]) if meta[:fence]
  warn(handler, :wrapper_tag_stripped, "stripped wrapper tags around the payload", *meta[:first_pos]) if meta[:wrapper]
  warn(handler, :suffix_text_ignored, "ignored non-JSON text after the payload", *meta[:last_pos]) if meta[:suffix]
end

.extract_payloads(input, options) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/smarter_json/parser.rb', line 409

def extract_payloads(input, options)
  payloads = candidate_ranges(input).filter_map do |range|
    slice = input.byteslice(range.begin, range.end - range.begin)
    begin
      SmarterJSON.send(:process_content, slice, options.merge(on_warning: nil))
      { slice: slice, range: range }
    rescue ParseError
      nil
    end
  end
  meta = wrapper_meta(input, payloads.map { |p| p[:range] })
  payloads.each { |payload| payload[:meta] = meta }
  payloads
end

.leading_label?(input) ⇒ Boolean

Whether the input opens with a bare “JSON:” / “Final answer:” label (which would otherwise parse, wrongly, as an implicit-root object keyed by the label). We use String#start_with? with a Regexp rather than match?(/A…/): start_with? checks only the beginning, whereas a A-anchored match? still retries at every byte position and so scans the WHOLE input (≈0.3s on a 200 MB document) on every parse. (Caller has already established the input is valid_encoding?.)

Returns:

  • (Boolean)


380
381
382
# File 'lib/smarter_json/parser.rb', line 380

def leading_label?(input)
  input.start_with?(/[[:space:]]*(?:JSON|Final answer)[[:space:]]*:/i)
end

.line_col_for(input, offset) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/smarter_json/parser.rb', line 456

def line_col_for(input, offset)
  line = 1
  col = 1
  i = 0
  while i < offset
    b = input.getbyte(i)
    break if b.nil?

    if b == LF
      line += 1
      col = 1
      i += 1
    elsif b == CR
      line += 1
      col = 1
      i += 1
      i += 1 if i < offset && input.getbyte(i) == LF
    else
      col += 1
      i += 1
    end
  end
  [line, col]
end

.non_payload_text(input, ranges) ⇒ Object



445
446
447
448
449
450
451
452
453
454
# File 'lib/smarter_json/parser.rb', line 445

def non_payload_text(input, ranges)
  out = +""
  pos = 0
  ranges.each do |range|
    out << input.byteslice(pos, range.begin - pos) if range.begin > pos
    pos = range.end
  end
  out << input.byteslice(pos, input.bytesize - pos) if pos < input.bytesize
  out
end

.process_string(input, options, &block) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/smarter_json/parser.rb', line 345

def process_string(input, options, &block)
  return SmarterJSON.send(:process_content, input, options, &block) unless input.valid_encoding?

  # Recovery is REACTIVE: parse first, and only fall back to wrapper extraction when
  # the parse actually fails (the rescue below). Every wrapper shape — code fences,
  # <json>/BEGIN_JSON tags, prose around the payload — makes the parse raise, so the
  # rescue catches it. Crucially this keeps clean input on the single-parse fast path
  # even when its string values legitimately contain ``` or <json> (real-world data
  # like GitHub event payloads is full of markdown), instead of dragging hundreds of
  # MB through the pure-Ruby candidate scan.
  #
  # The one exception is a bare leading label like "JSON: {...}", which parses
  # successfully but WRONGLY (as an implicit-root object keyed by the label), so it
  # must be intercepted before parsing.
  if leading_label?(input)
    payloads = extract_payloads(input, options)
    return replay_payloads(payloads, options, &block) unless payloads.empty?
  end

  SmarterJSON.send(:process_content, input, options, &block)
rescue ParseError => e
  raise if e.is_a?(EncodingError)

  payloads = extract_payloads(input, options)
  return replay_payloads(payloads, options, &block) unless payloads.empty?

  raise
end

.replay_payloads(payloads, options, &block) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/smarter_json/parser.rb', line 384

def replay_payloads(payloads, options, &block)
  handler = options[:on_warning]
  emit_wrapper_warnings(payloads, handler)

  results = payloads.map do |payload|
    SmarterJSON.send(:process_content, payload[:slice], options)
  end

  return results.each(&block).then { nil } if block_given?
  return nil if results.empty?
  return results.first if results.length == 1

  results
end

.substantive_text?(text) ⇒ Boolean

Returns:

  • (Boolean)


481
482
483
484
485
486
487
488
# File 'lib/smarter_json/parser.rb', line 481

def substantive_text?(text)
  return false if text.nil? || text.empty?

  stripped = text.dup
  stripped.gsub!(%r{/\*.*?\*/}m, "")
  stripped.gsub!(/^\s*(?:#|\/\/).*$/, "")
  !stripped.strip.empty? && !stripped.strip.match?(/\A(?:```[a-zA-Z0-9_-]*)?\z/) && !stripped.strip.match?(/\A(?:<\/?json>|BEGIN_JSON|END_JSON)\z/i)
end

.warn(handler, type, message, line, col) ⇒ Object



490
491
492
# File 'lib/smarter_json/parser.rb', line 490

def warn(handler, type, message, line, col)
  handler.call(Warning.new(type, message, line, col))
end

.wrapper_meta(input, ranges) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/smarter_json/parser.rb', line 424

def wrapper_meta(input, ranges)
  return { prefix: false, suffix: false, fence: false, wrapper: false } if ranges.empty?

  first = ranges.first
  last = ranges.last
  prefix = input.byteslice(0, first.begin)
  suffix = input.byteslice(last.end, input.bytesize - last.end)
  # Look for fence / wrapper markers only in the text we actually strip (outside
  # every recovered payload), so a ``` or <json> sitting inside a payload's own
  # string value does not trigger a "stripped a wrapper" warning.
  outside = non_payload_text(input, ranges)
  {
    prefix: substantive_text?(prefix),
    suffix: substantive_text?(suffix),
    fence: outside.include?("```"),
    wrapper: outside.match?(/<json\b|BEGIN_JSON\b/i),
    first_pos: line_col_for(input, first.begin),
    last_pos: line_col_for(input, last.begin)
  }
end