Coder Social home page Coder Social logo

Parse into array about hjson-php HOT 12 CLOSED

cdarken avatar cdarken commented on August 25, 2024
Parse into array

from hjson-php.

Comments (12)

cdarken avatar cdarken commented on August 25, 2024 2

I wouldn't say changing, just add an option to allow also arrays.
I will try, if I get some time.

from hjson-php.

cdarken avatar cdarken commented on August 25, 2024

I think I found a solution.
This seems to work:

$parser = new HJSONParser();
$jsonData = $parser->parse($json);
$arrays = json_decode(json_encode($jsonData), true);

Can you think of a caveat in this case?

from hjson-php.

laktak avatar laktak commented on August 25, 2024

parse() already returns an array:

$hjsonText="[1,2,3]";
$parser = new HJSONParser();
$v = $parser->parse($hjsonText);
print_r($v);

prints:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

from hjson-php.

cdarken avatar cdarken commented on August 25, 2024

Not if you have something more complex, like this:

{"some_key":"a","some_other_key":"b","settings":{"required":false}}

This will get parsed to this (output from var_dump):

object(stdClass)#809 (3) {
  ["some_key"]=>
  string(1) "a"
  ["some_other_key"]=>
  string(1) "b"
  ["settings"]=>
  object(stdClass)#813 (1) {
    ["required"]=>
    bool(false)
  }
}

from hjson-php.

laktak avatar laktak commented on August 25, 2024

Ah, I see. I'm not a PHP dev, this repo was contributed by @iainhallam and @jawb. Is the idiomatic php way to use arrays instead of objects?

from hjson-php.

cdarken avatar cdarken commented on August 25, 2024

Yes, usually we work with arrays. Also, I think the arguments should emulate the ones of json_decode.

from hjson-php.

laktak avatar laktak commented on August 25, 2024

@iainhallam Would you be OK with changing the API to arrays?

@cdarken Would you be willing to do a PR or do you know someone who would?

from hjson-php.

4cc355-d3n13d avatar 4cc355-d3n13d commented on August 25, 2024

Hey, this works fine for me:

$hjsonText="{"some_key":"a","some_other_key":"b","settings":{"required":false}}";
$parser = new HJSONParser();
$v = (array)$parser->parse($hjsonText);
print_r($v);

from hjson-php.

cdarken avatar cdarken commented on August 25, 2024

@4cc355-d3n13d it doesn't, the "setting" key has a stdClass instance as value, it should be array

from hjson-php.

laktak avatar laktak commented on August 25, 2024

also see #7

from hjson-php.

4cc355-d3n13d avatar 4cc355-d3n13d commented on August 25, 2024

@cdarken parse the array recursively

from hjson-php.

laktak avatar laktak commented on August 25, 2024

This is now implemented, thanks @nichtich !

from hjson-php.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.