Hash to_json sometimes seems to work and sometimes not, why?

This works:

person : Hash(String, String) = {"name" => "John", "age" => "30", "city" => "New York"}
p person.to_json

In the second case, I get:

In src/bakerstreet.cr:51:23

 51 | ARCHIVE = #{@dico.to_json}
                        ^------
Error: undefined method 'to_json' for Hash(String, String)

This doesn’t work

require "path"
require "dir"

class Baker

  def initialize(root : String)
    @root = root
    @root_path = Path.new(@root)
    @arc = "archive.cr"
    @dico = Hash(String, String).new
  end

  def to_path(file)
    return @root_path.join(file).to_s
  end

  # URL to archive path mapping
  def get_file_from_url?(url : String, bk, arc) : String
    if url == "/" || url == ""
      url = "/index.html"
    end
    begin
      content = arc[bk.to_path(url)]
      return content
    rescue exception
      return "File not found: #{bk.to_path(url)}"
    end
  end

  # initialize the Hash '@dico'
  def walk_files_and_directories(directory : String)
    Dir.each_child(directory) do |entry|
      path = File.join(directory, entry)
      if File.directory?(path)
        walk_files_and_directories(path)
      else
        content = File.read(path)
        @dico[path.to_s] = content
      end
    end
  end

  # write 'archive' to disk
  def make_archive
    walk_files_and_directories(@root)

    code_litteral = "
    ARCHIVE = #{@dico.to_json}
    "
    File.write(@arc, code_litteral)
  end # def make_archive


end # class

You didn’t require json

Thank you…
:slight_smile: