# Jsonable interface

- Thread: https://externals.io/message/48324
- Original archive: https://news-web.php.net/php.internals/48324
- Started by Mathias Grimm on 2010-05-18T21:04:51+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #48324 — Mathias Grimm — 2010-05-18T21:04:51+00:00

URL: https://externals.io/message/48324#48324

If php have annotations feature its be very simple.

class MyClass implements Jsonable
{
    private $name;
    private $age;
    Transient private $dtCreated;// or JsonIgnore or JsonTransient
}

all properties will be encoded, except transient.

Otherwise,php can have jasonable interface and class that implements it, by
default encode all properties(private,default,public,protected), except
properties in the __jsonTransient.

class MyClass implements Jsonable
{
    private $name;
    private $age;
    private $id;

    public function __jsonTransient()
    {
        return array('id');
    }
}

-- 
Mathias Grimm

Sun Certified Java Programmer 6.0 #SUN604760
Zend Certified Engineer #ZEND006756

http://mathiasgrimm.com.br
http://phpempregos.com.br

---

## #48326 — Sara Golemon — 2010-05-18T21:22:26+00:00

URL: https://externals.io/message/48324#48326 · In reply to: #48324

Mathias Grimm wrote:
> If php have annotations feature its be very simple.
> 
>     Transient private $dtCreated;// or JsonIgnore or JsonTransient
> 
[*snip*]

This seems to be an attempt to solve a subset of the problem solved by 
JSONSerializable, but from a negation rather than a explicit 
orientation.  What this doesn't provide, is the ability to serialize an 
object with modified or even completely different data from what is 
found in the base properties (something which may well be desired).

This approach also has the very obvious added workload of introducing 
property traits (something I'm not against, but would require a great 
deal more discussion).

Given that it does less at higher cost, I'm inclined to reply with "meh".

-Sara

---

## #48328 — Mathias Grimm — 2010-05-18T21:36:22+00:00

URL: https://externals.io/message/48324#48328 · In reply to: #48326

_json function to override properties and jsonIgore properties

when call json_encode($obMyClass) the __json and __jsonIgore will be called.

if a class is jsonable, this means that all properties will be encoded,
except properties on the ignore list


> This seems to be an attempt to solve a subset of the problem solved by
> JSONSerializable, but from a negation rather than a explicit orientation.
>  What this doesn't provide, is the ability to serialize an object with
> modified or even completely different data from what is found in the base
> properties (something which may well be desired).
>
> This approach also has the very obvious added workload of introducing
> property traits (something I'm not against, but would require a great deal
> more discussion).
>
> Given that it does less at higher cost, I'm inclined to reply with "meh".
>
> -Sara
>
> --
> 
> 
>
>


-- 
Mathias Grimm

Sun Certified Java Programmer 6.0 #SUN604760
Zend Certified Engineer #ZEND006756

http://mathiasgrimm.com.br
http://phpempregos.com.br

---

## #48331 — Sara Golemon — 2010-05-18T21:56:04+00:00

URL: https://externals.io/message/48324#48331 · In reply to: #48328

I didn't say that I didn't understand the purpose of it.  I said that it 
provides less functionality than what we already have implemented.

Mathias Grimm wrote:
> _json function to override properties and jsonIgore properties
> 
> when call json_encode($obMyClass) the __json and __jsonIgore will be called.
> 
> if a class is jsonable, this means that all properties will be encoded,
> except properties on the ignore list
> 
> 
> > […]

---

## #48337 — Mathias Grimm — 2010-05-19T00:44:25+00:00

URL: https://externals.io/message/48324#48337 · In reply to: #48331

I had an php 5.3 branch, now i checkout the trunk and can see the
jsonSerializable.




> I didn't say that I didn't understand the purpose of it.  I said that it
> provides less functionality than what we already have implemented.
>
>
-- 
Mathias Grimm

Sun Certified Java Programmer 6.0 #SUN604760
Zend Certified Engineer #ZEND006756

http://mathiasgrimm.com.br
http://phpempregos.com.br
