Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:13:18+00:00 2026-06-01T02:13:18+00:00

I’m having an issue with passing the generated JSON notation of my object to

  • 0

I’m having an issue with passing the generated JSON notation of my object to my Sinatra application. The problem I have is twofold:

  • I have 2 classes that are mapped to a database using the Sequel gem. When they generate JSON it is ok and properly implemented.
  • I have a custom class called registration that maps one of the classes with an additional field. The goal is to generate JSON out of this and pass that JSON to the application using cucumber (test purpose)

The application code responsible for handling the request has the following function defined:

post '/users' do
  begin
    hash = JSON.parse(self.request.body.read)
    registration = Registration.new.from_json(@request.body.read)
    registration.user.country = Database::Alaplaya.get_country_by_iso_code(registration.user.country.iso_code)
    return 400 unless(registration.is_valid?)
    id = Database::Alaplaya.create_user(registration.user)

    # If the registration failed in our system, return a page 400.
    return 400 if id < 1
end
  • problem 1: I cannot use the params hash. It exists but is just an empty hash. Why?
  • problem 2: I cannot deserialize the JSON generated by the class itself. Why?

The registration class looks like this:

require 'json'

class Registration
  attr_accessor :user, :project_id

  def to_json(*a)
    {
        'json_class'   => self.class.name,
        'data'         => [@user.to_json(*a), @project_id]
    }.to_json(*a)
  end

  def self.json_create(o)
    new(*o['data'])
  end

  # Creates a new instance of the class using the information provided in the
  # hash. If a field is missing in the hash, nil will be assigned to that field
  # instead.
  def initialize(params = {})
    @user = params[:user]
    @project_id = params[:project_id]
  end

  # Returns a string representing the entire Registration.
  def inspect
    "#{@user.inspect} - #{@user.country.inspect} - #{@project_id}"
  end

  # Returns a boolean valid representing whether the Registration instance is
  # considered valid for the API or not. True if the instance is considered
  # valid; otherwise false.
  def is_valid?
    return false if @user.nil? || @project_id.nil?
    return false if !@user.is_a?(User) || !@project_id.is_a?(Fixnum)
    return false if !@user.is_valid?
    true
  end
end

I had to implement the methods to generate the JSON output correctly. When I run this in console I get the following output generated:

irb(main):004:0> r = Registration.new(:user => u, :project_id => 1)
=> new_login - nil - 1
irb(main):005:0> r.to_json
=> "{\"json_class\":\"Registration\",\"data\":[\"{\\\"json_class\\\":\\\"User\\\
",\\\"login\\\":\\\"new_login\\\"}\",1]}"

Which looks like valid JSON to me. However when I POST this to the application server and try to parse this, JSON complains that at least 2 octets are needed and refuses to deserialize the object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:13:19+00:00Added an answer on June 1, 2026 at 2:13 am

    If you’re using Sequel as your ORM, try something like this:

    In your model:

    class Registration < Sequel::Model
      many_to_one :user
      many_to_one :project
      plugin :json_serializer
    end        
    

    The server:

    before do
      @data = JSON.parse(request.body.read) rescue {}
    end
    
    post '/users' do
      @registration = Registration.new @data
      if @registration.valid?
        @registration.save 
        @registration.to_json #return a JSON representation of the resource
      else
        status 422 #proper status code for invalid input
        @registration.errors.to_json
      end
    end
    

    I think you may be overcomplicating your registration process. If the HTTP action is POST /users then why not create a user? Seems like creating a registration is overly complex. Unless your user already exists, in which case POST /users would be incorrect. If what you’re really intending to do is add a user to to a project, then you should PUT /projects/:project_id/users/:user_id and the action would look something like this:

    class User < Sequel::Model
      many_to_many :projects
    end
    class Project < Sequel::Model
      many_to_many :users
    end
    #make sure your db schema has a table called users_projects or projects_users
    
    put '/projects/:project_id/users/:user_id' do
      #find the project
      @project = Project.find params[:project_id]
      raise Sinatra::NotFound unless @project
      #find the user
      @user = Project.find params[:project_id]
      raise Sinatra::NotFound unless @user
      #add user to project's users collection
      @project.add_user @user
      #send a new representation of the parent resource back to the client
      #i like to include the child resources as well
      #json might look something like this
      #{ 'name' : 'a project name', 'users' : ['/users/:user_id', '/users/:another_user_id'] }
      @project.to_json
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.