Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javax.json.JsonException: JSON validation provider was not found. #18

Closed
Swisyn opened this issue May 31, 2019 · 2 comments
Closed

javax.json.JsonException: JSON validation provider was not found. #18

Swisyn opened this issue May 31, 2019 · 2 comments
Assignees

Comments

@Swisyn
Copy link

Swisyn commented May 31, 2019

Hello,

I really don't have any idea why am I getting that issue at all. What could be the issue? I've created a schema validator for Android with this library but even everything seems fine, I still get that issue.

the exception was;
javax.json.JsonException: JSON validation provider was not found. at com.kobil.ui.validator.spi.JsonValidationProvider.provider(JsonValidationProvider.java:51) at com.kobil.ui.validator.api.JsonValidationService$-CC.newInstance(JsonValidationService.java:84)

the class:
`
class JsonSchemaHelper {
private var context: Context
private var jsonString: String? = null
private var schemaString: String? = null

constructor(context: Context) {
    this.context = context
}

constructor(context: Context, jsonString: String? = null) {
    this.context = context
    this.jsonString = jsonString
}

constructor(context: Context, jsonString: String? = null, schema: String? = null) {
    this.context = context
    this.jsonString = jsonString
    this.schemaString = schema
}

fun isJsonValid(callback: ValidationListener? = null): Boolean {
    var result = false

    if (schemaString != null && schemaString!!.isNotEmpty()) {
        result = validate(schemaString!!, callback)
    } else {
        context.resources.openRawResource(R.raw.service_provider_v1_combined)
            .bufferedReader(Charsets.UTF_8).use {
                result = validate(it.readText(), callback)
            }
    }
    return result
}

fun isJsonValid(): Boolean {
    return isJsonValid(null)
}

private fun validate(
    schemaText: String? = null,
    callback: ValidationListener? = null
): Boolean {
    var result = true

    val service: JsonValidationService = JsonValidationService.newInstance()

    if (schemaText != null && schemaText.isNotEmpty()) {
        val jsonSchema =
            service.readSchema(ByteArrayInputStream(schemaText.toByteArray(Charsets.UTF_8)))

        var jsonStringIs: InputStream? = null

        if (jsonString != null) {
            if (jsonString!!.isNotEmpty()) {

                jsonStringIs =
                    ByteArrayInputStream(jsonString!!.toByteArray(Charsets.UTF_8))
            } else {
                callback?.onError("Json is null, did you forget to pass it?")
                result = false
            }
        }

        if (jsonSchema == null) {
            callback?.onError("Json schema object is null")
            result = false
        }

        if (jsonStringIs == null) {
            callback?.onError("Json input source is null")
            result = false
        } else {
            service.createParser(
                jsonStringIs,
                jsonSchema,
                service.createProblemPrinter {
                    callback?.onError(it)
                    result = false

                    Log.e("handler", it)
                    println("handler $it")
                    System.out.println("handler $it")
                }).use { parser ->
                try {
                    while (parser.hasNext()) {
                        val event =
                            parser.next() // this part needed for catching issues too even that field is useless!

// Log.e("event", event.name)
// println(event.toString())
// System.out.println(event.toString())
}
} catch (e: JsonParsingException) {
result = false
e.message?.let { callback?.onError(it) }
}
}

        }
    } else {
        result = false
        callback?.onError("Json schema text was null")
    }

    if (result) callback?.onSuccess()

    return result
}

interface ValidationListener {
    fun onError(errorDescription: String? = null)
    fun onSuccess()
}

}
`

@leadpony
Copy link
Owner

Hello @Swisyn
Thank you for trying this library.
For achieving loose coupling between the API and the implementation, this library uses Java Service Provider Interface which looks up the proper implementation via META-INF/services/ or module-info.java. Judging from the exception log, the package of JsonValidationProvider seems to be renamed from my original. If my guess is correct, you must modify the configuration of the provider which resides where as described above.

@anitadc
Copy link

anitadc commented Dec 2, 2019

I am facing same problem when running Spring boot application jar from command prompt. But It is working fine from eclipse and same application jar in other machine. I am using java11 and no module structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants