| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (c) Nate Brunette. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Distributed under the MIT License (http://opensource.org/licenses/MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Tebru\Gson; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Tebru\Gson\Element\JsonArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Tebru\Gson\Element\JsonElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Tebru\Gson\Element\JsonNull; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tebru\Gson\Element\JsonObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Tebru\Gson\Element\JsonPrimitive; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class JsonElementTypeAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Nate Brunette <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class JsonElementTypeAdapter extends TypeAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Read the next value, convert it to its type and return it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param JsonReadable $reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return JsonElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @throws \LogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function read(JsonReadable $reader): JsonElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         switch ($reader->peek()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             case JsonToken::BEGIN_OBJECT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 14 |  |                 $object = new JsonObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $reader->beginObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 14 |  |                 while ($reader->hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 14 |  |                     $name = $reader->nextName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |                     $object->add($name, $this->read($reader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |                 $reader->endObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  |                 return $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             case JsonToken::BEGIN_ARRAY: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |                 $array = new JsonArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $reader->beginArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |                 while ($reader->hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 13 |  |                     $array->addJsonElement($this->read($reader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |                 $reader->endArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |                 return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             case JsonToken::STRING: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |                 return JsonPrimitive::create($reader->nextString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             case JsonToken::NUMBER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |                 return JsonPrimitive::create($reader->nextDouble()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 12 |  |             case JsonToken::BOOLEAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |                 return JsonPrimitive::create($reader->nextBoolean()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 7 |  |             case JsonToken::NULL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |                 $reader->nextNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |                 return new JsonNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 throw new LogicException(\sprintf('Could not handle token "%s" at "%s"', $reader->peek(), $reader->getPath())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Write the value to the writer for the type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param JsonWritable $writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param JsonElement $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     public function write(JsonWritable $writer, $value): void | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         if (null === $value || $value->isJsonNull()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $writer->writeNull(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 10 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 10 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 3 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if ($value->isJsonObject()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 3 |  |             $writer->beginObject(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             foreach ($value->asJsonObject() as $key => $element) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 $writer->name($key); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 8 |  |                 $this->write($writer, $element); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 2 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 2 |  |             $writer->endObject(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 2 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         if ($value->isJsonArray()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 2 |  |             $writer->beginArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             foreach ($value->asJsonArray() as $element) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 $this->write($writer, $element); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 8 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 2 |  |             $writer->endArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 2 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         if ($value->isInteger()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 2 |  |             $writer->writeInteger($value->asInteger()); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 8 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         if ($value->isFloat()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 1 |  |             $writer->writeFloat($value->asFloat()); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 7 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         if ($value->isBoolean()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 1 |  |             $writer->writeBoolean($value->asBoolean()); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 6 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         $writer->writeString($value->asString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |