Coder Social home page Coder Social logo

gson-php's People

Contributors

deegital avatar gounlaf avatar mattjanssen avatar mloberg avatar natebrunette avatar nheimann1 avatar samnela avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gson-php's Issues

Fix how type resolution works with accessors

We should look at getters/setters first depending on the direction and properties last unless there's a @Type annotation.

The order should be

  • @Type
  • getter/setter
  • property
  • setter/getter
  • docblock getter/setter
  • docblock property
  • docblock setter/getter

Integer-keyed Generics Don't Throw Exception on String Key

If I have the follow type declaration @Gson\Type("array<int, string>") and I read the following JSON {"foo": "bar"} the JsonDecodeReader converts the "foo" into null because "foo" doesn't get wrapped in quotes before it's passed to json_decode(). This then causes the IntegerTypeAdapter to not throw an exception, because null is a valid value.

The expected behavior would be to throw an UnexpectedJsonTokenException.

This is happening because the ArrayTypeAdapter only checks if $keyType->isString() when deciding if it should wrap the key name in quotes.

A solution would be to also check if !is_numeric($name) when deciding to wrap it in quotes. As booleans and null aren't valid keys in PHP or JSON, we don't need extra checks for those.

What's the right way of using annotations?

Hey,
I want to start saying that this looks like a really nice package, it was needed for a long time.

I have a doubt about annotations, since docs are no so clear on how to use it.

I have created a class, which has a single property, and i want to use SerializedName to map the json attribute to the class property:

class User
{
   /**
    * @SerializedName("id")
    */
   protected $userId;
}

I've done several variations of the above code (with \ as first char, with the whole namespace, with the use at the beginning of the class) and all ends on pretty much the same output:
[Semantical Error] The annotation \"@Tebru\\Gson\\Annotation\\SerializedName\" in property User::$userId does not exist, or could not be auto-loaded.

The only two things that worked so far are:

  • class_exists(SerializedName::class); at the beginning of the execution (feels dirty)
  • AnnotationRegistry::registerLoader('class_exists'); based on this thread (feels less dirty, but smells anyways)

I've seen that AnnotationRegistry is referenced on the tests, is that the actual fix for my problem?

Thanks

[QUESTION] Does the order of elements in associative array is guaranteed between encode/decode?

I've got a specific use case where I am calculating checksum/hash for the object contents based on the serialization output. Does GSON preserve the order of elements in an associative array?
For instance JSON doesn't support that. I've found many references that GSON maintains that order correctly, however I can't find any official info about that.
Hope you can help to answer that. Thanks !

Weird Capitalization error

So I can't seem to get it to deserialize variables that have capital letters in them without specifying the SerializedName annotation.

The following does not work.

/**
 * @var integer
 */
public $testTest;

Consider migrating to the GitHub Action

As title, since the Travis CI building is very slow, I think it's time to migrate to the GitHub actions.

Once this issue has been accepted by repository owner, I will be happy to work on that.

Add support for PHP 7.4 typed properties while type guessing

The TypeTokenFactory could be fixed and enhanced to support PHP typed properties.

Fix:

The following currently throws an exception if it encounters a non-initialized typed property:

$property->getDeclaringClass()->getDefaultProperties()[$property->getName()];

See https://3v4l.org/gVdKO to illustrate why this happens.
An easy fix would be to add ?? null to the end.

Enhancement:

Somewhere in this chain of guessing the property's type could be a check for the property type:

$propertyAsTyped = $property->getType()->getName();

Obviously it would need a PHP version check around execution, and a null check in case it's not typed.

Null return type required

On getter functions I have to allow nulls in the type declaration ?MySpecialClass. I tried specifying the type using annotations but the getter still gets called before there is data in the variable it's calling.

/**
 * @return string
 */
public function getNumber(): ?string
{
    return $this->number;
}

I would like to do this

/**
 * @return string
 */
public function getNumber(): string
{
    return $this->number;
}

Documention?

I know it's an early work, but it would be nice to have some examples on how to use this package.

Function ReflectionType::__toString() is deprecated

I'm trying to use gson-php: 0.7.1 and I'm receiving this error due to __toString is deprecated as of php 7.1

this error is thrown in TypeTokenFactory in line 98
$setterType = TypeToken::create((string)$parameter->getType());

though instead of casting to string getName() should be called.

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.