Completed
Pull Request — master (#869)
by Asmir
10:42
created

JsonDeserializationVisitor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 31.25%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 30
ccs 5
cts 16
cp 0.3125
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C decode() 0 27 7
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer;
20
21
use JMS\Serializer\Exception\RuntimeException;
22
23
class JsonDeserializationVisitor extends GenericDeserializationVisitor
24
{
25 57
    protected function decode($str)
26
    {
27 57
        $decoded = json_decode($str, true);
28
29 57
        switch (json_last_error()) {
30 57
            case JSON_ERROR_NONE:
31 57
                return $decoded;
32
33
            case JSON_ERROR_DEPTH:
34
                throw new RuntimeException('Could not decode JSON, maximum stack depth exceeded.');
35
36
            case JSON_ERROR_STATE_MISMATCH:
37
                throw new RuntimeException('Could not decode JSON, underflow or the nodes mismatch.');
38
39
            case JSON_ERROR_CTRL_CHAR:
40
                throw new RuntimeException('Could not decode JSON, unexpected control character found.');
41
42
            case JSON_ERROR_SYNTAX:
43
                throw new RuntimeException('Could not decode JSON, syntax error - malformed JSON.');
44
45
            case JSON_ERROR_UTF8:
46
                throw new RuntimeException('Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)');
47
48
            default:
49
                throw new RuntimeException('Could not decode JSON.');
50
        }
51
    }
52
}
53