-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
### Concept | ||
|
||
A Handler is an implementation of the `PibifyGenerated<T>` abstract class. It primarily has 2 methods | ||
|
||
```java | ||
public abstract T deserialize(IDeserializer deserializer, Class<T> type, SerializationContext context) | ||
throws PibifyCodeExecException; | ||
|
||
public abstract void serialize(T object, ISerializer serializer, SerializationContext context) | ||
throws PibifyCodeExecException; | ||
``` | ||
|
||
The Pibify Maven plugin generates a concrete implementation of this abstract class. | ||
|
||
For every class in the maven module annotated with `@Pibify`, a handler is generated. The handler is named as | ||
`<ClassName>Handler`. | ||
|
||
### Out of the Box Handlers | ||
|
||
Few handlers are provided out of the box. They are: | ||
|
||
1. PibifyObjectHandler - for cases where the reference type is `java.lang.Object` | ||
2. PibifyMapHandler - for cases where the reference type is `java.util.Map` with missing type parameters | ||
3. PibifyCollectionHandler - for cases where the reference type is `java.util.Collection` with missing type parameters | ||
4. BigDecimalHandler - for cases where the reference type is `java.math.BigDecimal` | ||
5. DateHandler - for cases where the reference type is `java.util.Date` | ||
|
||
This well-known handlers are registered in `AbstractPibifyHandlerCache`'s protected constructor. | ||
|
||
#### Adding custom hand-written handlers | ||
|
||
If you want to add custom handlers, you can do so by extending the `AbstractPibifyHandlerCache` class and registering | ||
your custom handlers in the constructor. | ||
|
||
```java | ||
public class CustomHandlerCache extends AbstractPibifyHandlerCache { | ||
public CustomHandlerCache() { | ||
super(); | ||
// Type is the class type of the object | ||
super.mapBuilder.put(type, new CustomHandler()); | ||
} | ||
} | ||
``` | ||
|
||
Then merge `CustomHandlerCache` with the generated `PibifyHandlerCache` using `MultiModuleHandlerCache`: | ||
|
||
```java | ||
new MultiModulePibifyHandlerCache(new CustomHandlerCache(), new PibifyHandlerCache()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
pibify-core/src/main/java/com/flipkart/pibify/codegen/stub/DateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* | ||
* *Copyright [2025] [Original Author] | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * http://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.flipkart.pibify.codegen.stub; | ||
|
||
import com.flipkart.pibify.codegen.PibifyCodeExecException; | ||
import com.flipkart.pibify.serde.IDeserializer; | ||
import com.flipkart.pibify.serde.ISerializer; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
/** | ||
* This class is used for serde of Date | ||
* Author bageshwar.pn | ||
* Date 15/01/25 | ||
*/ | ||
public class DateHandler extends PibifyGenerated<Date> { | ||
|
||
@Override | ||
public void serialize(Date object, ISerializer serializer, SerializationContext context) throws PibifyCodeExecException { | ||
if (object != null) { | ||
try { | ||
serializer.writeLong(1, object.getTime()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Date deserialize(IDeserializer deserializer, Class<Date> type, SerializationContext context) throws PibifyCodeExecException { | ||
try { | ||
int tag = deserializer.getNextTag(); | ||
Date date = null; | ||
while (tag != 0 && tag != PibifyGenerated.getEndObjectTag()) { | ||
switch (tag) { | ||
case 8: | ||
date = new Date(deserializer.readLong()); | ||
break; | ||
default: | ||
break; | ||
} | ||
tag = deserializer.getNextTag(); | ||
} | ||
return date; | ||
} catch (IOException e) { | ||
throw new PibifyCodeExecException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters