Comments (8)
Just stumbled upon this myself. They work with native default values IF they come after ones without default values.
from serde.
Resolved in #31. I'm shocked that bug managed to survive so long, honestly. I could have sworn there was a test for that case. But it's been resolved, and supports null as a default value, too. (Which required some fun enum dancing.)
from serde.
The Field
default should work for any field type. SequenceField
et al are TypeFields, which are type-specific sub-attributes. You absolutely can and should use both Field
and SequenceField
on the same property.
from serde.
Sprinkled some debugs into the Serde source and for the test case above the shouldUseDefault property currently always evaluates to false
. After forcing it to true and using a default value other than null
it worked as expected.
from serde.
The tests already cover a whole bunch of default values. Is this limited to when the default value being set is null, perhaps?
from serde.
Also, try on the master
branch. There's been a lot of improvements around null handling recently that may have already resolved this.
from serde.
The tests already cover a whole bunch of default values. Is this limited to when the default value being set is null, perhaps?
No luck. Tested with current master:
class Foo
{
public function __construct(
#[Field(default: "42")]
public string $bar,
public string $name
) {
//
}
}
$serde = new SerdeCommon();
dump($serde->deserialize(['name' => 'foobar'], 'array', Foo::class));
Foo {#6350
+name: "foobar"
}
$ composer show -t crell/serde
crell/serde dev-master A general purpose serialization and deserialization library
├──crell/attributeutils ~0.8.2
│ ├──crell/fp ~0.4.0
│ │ └──php ~8.1
│ └──php ~8.1
├──crell/fp >= 0.3.3
│ └──php ~8.1
└──php ~8.1
from serde.
Thanks for the swift fix! One more thing I noticed while working with defaults, would it make sense to also add a default
arg to SequenceField
and DictionaryField
? It felt a bit inconsistent to me.
from serde.
Related Issues (20)
- Null value not set on deserialization HOT 4
- Improve developer's tooling HOT 1
- mixed fails to serialize objects HOT 7
- Using string-backed enums in type maps HOT 5
- TypeError when serializing a flattened nullable property HOT 3
- Serde should enforce SequenceField declaration HOT 10
- Why does `requireValue` default to false? HOT 3
- Can I add TOML support? HOT 3
- Explore using JetBrains' ArrayShape attribute
- Better support for composite value objects HOT 6
- Ability to flatten single property objects HOT 2
- Alias for Inner Objects HOT 1
- Possible improvement to the Dict explode functionality
- Problem with abstract nested object HOT 3
- Consider parsing ints for deserializing Dates HOT 1
- Deserializing top-level sequence HOT 2
- Deserializing NULL values is ignored HOT 2
- Support for default class settings without explicitly adding attributes HOT 2
- Document TypeFields
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serde.