Coder Social home page Coder Social logo

json-mapper's Issues

Mapping a nested Json object

What's the best way to map a nested json object?

Imagine this response shown here: https://stytch.com/docs/b2b/api/create-organization

Inside that Json object is an Organisation, which is the only thing I need to map.

Currently, I json_decode the response to test for errors and raise exceptions based on any error object. This means I have access to an organisation object already ($data->organisation).

I don't think re-json_encoding that Organisation object is the way forward, only to pass it to Json-Mapper to do it all again.

So from what I can see, I'm either petitioning for:

  • mapToObject() to be made public, so I can pass my object straight to it; or,
  • map() gets a new, nullable, argument that specifies the path to the object to map.

Am I seeing this from the wrong angle? All thoughts appreciated!

Edit: similar question, but for when an array of objects is returned.

Edit2: Would it be typical to leave json-mapper alone, and create a PHP object per API Response type?

Issue with matching properties' names

If you have different props with the matching string in the beginning of their names (and at least two of them are PHPdoc-ed with @param), you get an error at src/Reflection/Reflector.php::149 which is incorrect.

Please, consider adding \W to the end of regular expression at src/Reflection/Reflector.php::149:

$pattern = '/@param(.*)\$' . $parameter->getName() . '\W/';

Brick \ JsonMapper \ JsonMapperException Unexpected property "labels" in JSON data: Google\Service\MyBusinessBusinessInformation\Location::__construct() does not have a corresponding $labels parameter. If you want to allow extra properties, change the $onExtraProperties option.

That class is declared as:

namespace Google\Service\MyBusinessBusinessInformation;

class Location extends \Google\Collection
{
  protected $collection_key = 'serviceItems';
  protected $adWordsLocationExtensionsType = AdWordsLocationExtensions::class;
  protected $adWordsLocationExtensionsDataType = '';
  protected $categoriesType = Categories::class;
  protected $categoriesDataType = '';
  /**
   * @var string[]
   */
  public $labels;

...
See: https://github.com/googleapis/google-api-php-client-services/blob/main/src/MyBusinessBusinessInformation/Location.php#L30C12-L30C12

HUGE PROBLEM: class ImportResolver is missing

Class "Brick\Reflection\ImportResolver" not found

row 289 of Reflection/Reflector.php file

private function convertNamedType(string $namedType, bool $isReflection, ReflectionParameter $reflectionParameter): SimpleType|ClassType|EnumType
    {
        $namedTypeLower = strtolower($namedType);

        $isAllowedBuiltinType = in_array($namedTypeLower, self::ALLOWED_BUILTIN_TYPES, true);
        $isDisallowedBuiltinType = in_array($namedTypeLower, self::DISALLOWED_BUILTIN_TYPES, true);
        $isBuiltinType = $isAllowedBuiltinType || $isDisallowedBuiltinType;

        if (! $isBuiltinType && ! $isReflection) {
            // Class names coming from reflection are already fully qualified, while class names coming from @param
            // must be resolved according to the current namespace & use statements,
            $importResolver = new ImportResolver($reflectionParameter); <--- HERE

            $namedType = $importResolver->resolve($namedType);
            $namedTypeLower = strtolower($namedType);
        }

Show JSON path in exceptions

Exceptions such as "Unexpected property %s in JSON data" should display the full path to the property in the JSON document, for easier debugging.

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.