| 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 |  |  |                 $object = new JsonObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $reader->beginObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 while ($reader->hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     $name = $reader->nextName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     $object->add($name, $this->read($reader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $reader->endObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 return $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             case JsonToken::BEGIN_ARRAY: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $array = new JsonArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $reader->beginArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 while ($reader->hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     $array->addJsonElement($this->read($reader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $reader->endArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             case JsonToken::STRING: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 return JsonPrimitive::create($reader->nextString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             case JsonToken::NUMBER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 return JsonPrimitive::create($reader->nextDouble()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             case JsonToken::BOOLEAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 return JsonPrimitive::create($reader->nextBoolean()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             case JsonToken::NULL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $reader->nextNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 return new JsonNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             default: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |                 throw new LogicException(\sprintf('Could not handle token "%s" at "%s"', $reader->peek(), $reader->getPath())); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if ($value->isJsonObject()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $writer->beginObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             foreach ($value->asJsonObject() as $key => $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $writer->name($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $this->write($writer, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $writer->endObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if ($value->isJsonArray()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $writer->beginArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             foreach ($value->asJsonArray() as $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->write($writer, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $writer->endArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if ($value->isInteger()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $writer->writeInteger($value->asInteger()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if ($value->isFloat()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $writer->writeFloat($value->asFloat()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if ($value->isBoolean()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $writer->writeBoolean($value->asBoolean()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |         $writer->writeString($value->asString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |