How I could get a primitive boolean from a boolean object

I;ve an object (it’s a response I get from a C3 network request) as follows:

var boolObj = {
  'type': 'boolean',
  'value': true
}

When I try to convert this to a boolean with c3Make('boolean', boolObj) I get the same original object and not a primitive boolean value.

How I could get a primitive boolean without having to call boolObj.value?

With the below (both in the static console and on the server), I get a type of "boolean":

var boolObj = {
  'type': 'boolean',
  'value': true
}
typeof c3Make('boolean', boolObj)

Yeah it is a boolean but apparently not a primitive, e.g. the example below prints true

var boolObj = {
  'type': 'boolean',
  'value': false
}

if(c3Make('boolean', boolObj)) {
  console.log(true)
}else {
  console.log(false)
}

That’s interesting because on the latest 7.9, your snippet prints false.
Which version are you working on?

I’m on development version 7.10, my snippet worked last Thursday, the next day I noticed the new behavior. I was doubting that it never worked, but you confirm it was properly working!!!

Same false result on 7.10 development branch.

Since type is “boolean” (primitive), I would expect c3Make to produce the JavaScript value true from your first example. This isn’t how c3Make is generally used though, so there could well be a bug. (c3Make is generally used to build Obj instances.)

thanks @JohnCoker for your feedback, what would you suggest alternatively to get a boolean out of this boolObj? is boolObj.value the only option?

I don’t know what you’re trying to do, so I can’t make a simple suggestion. Generally the client handles deserialization of server values for you. If you’re going to do it manually, there are many cases to handle besides this one.

It may be that the simplest answer is to fix the bug so that c3Make returns an unboxed (primitive) boolean value and then use c3Make for everything.

1 Like